netfilter: ip6table_raw: fix table priority
[safe/jmp/linux-2.6] / net / netlabel / netlabel_unlabeled.c
index d0c628c..852d9d7 100644 (file)
@@ -10,7 +10,7 @@
  */
 
 /*
- * (c) Copyright Hewlett-Packard Development Company, L.P., 2006 - 2007
+ * (c) Copyright Hewlett-Packard Development Company, L.P., 2006 - 2008
  *
  * This program is free software;  you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
@@ -54,6 +54,7 @@
 #include <asm/atomic.h>
 
 #include "netlabel_user.h"
+#include "netlabel_addrlist.h"
 #include "netlabel_domainhash.h"
 #include "netlabel_unlabeled.h"
 #include "netlabel_mgmt.h"
@@ -76,22 +77,20 @@ struct netlbl_unlhsh_tbl {
        struct list_head *tbl;
        u32 size;
 };
+#define netlbl_unlhsh_addr4_entry(iter) \
+       container_of(iter, struct netlbl_unlhsh_addr4, list)
 struct netlbl_unlhsh_addr4 {
-       __be32 addr;
-       __be32 mask;
        u32 secid;
 
-       u32 valid;
-       struct list_head list;
+       struct netlbl_af4list list;
        struct rcu_head rcu;
 };
+#define netlbl_unlhsh_addr6_entry(iter) \
+       container_of(iter, struct netlbl_unlhsh_addr6, list)
 struct netlbl_unlhsh_addr6 {
-       struct in6_addr addr;
-       struct in6_addr mask;
        u32 secid;
 
-       u32 valid;
-       struct list_head list;
+       struct netlbl_af6list list;
        struct rcu_head rcu;
 };
 struct netlbl_unlhsh_iface {
@@ -204,26 +203,28 @@ static void netlbl_unlhsh_free_addr6(struct rcu_head *entry)
 static void netlbl_unlhsh_free_iface(struct rcu_head *entry)
 {
        struct netlbl_unlhsh_iface *iface;
-       struct netlbl_unlhsh_addr4 *iter4;
-       struct netlbl_unlhsh_addr4 *tmp4;
-       struct netlbl_unlhsh_addr6 *iter6;
-       struct netlbl_unlhsh_addr6 *tmp6;
+       struct netlbl_af4list *iter4;
+       struct netlbl_af4list *tmp4;
+#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
+       struct netlbl_af6list *iter6;
+       struct netlbl_af6list *tmp6;
+#endif /* IPv6 */
 
        iface = container_of(entry, struct netlbl_unlhsh_iface, rcu);
 
        /* no need for locks here since we are the only one with access to this
         * structure */
 
-       list_for_each_entry_safe(iter4, tmp4, &iface->addr4_list, list)
-               if (iter4->valid) {
-                       list_del_rcu(&iter4->list);
-                       kfree(iter4);
-               }
-       list_for_each_entry_safe(iter6, tmp6, &iface->addr6_list, list)
-               if (iter6->valid) {
-                       list_del_rcu(&iter6->list);
-                       kfree(iter6);
-               }
+       netlbl_af4list_foreach_safe(iter4, tmp4, &iface->addr4_list) {
+               netlbl_af4list_remove_entry(iter4);
+               kfree(netlbl_unlhsh_addr4_entry(iter4));
+       }
+#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
+       netlbl_af6list_foreach_safe(iter6, tmp6, &iface->addr6_list) {
+               netlbl_af6list_remove_entry(iter6);
+               kfree(netlbl_unlhsh_addr6_entry(iter6));
+       }
+#endif /* IPv6 */
        kfree(iface);
 }
 
@@ -246,59 +247,6 @@ static u32 netlbl_unlhsh_hash(int ifindex)
 }
 
 /**
- * netlbl_unlhsh_search_addr4 - Search for a matching IPv4 address entry
- * @addr: IPv4 address
- * @iface: the network interface entry
- *
- * Description:
- * Searches the IPv4 address list of the network interface specified by @iface.
- * If a matching address entry is found it is returned, otherwise NULL is
- * returned.  The caller is responsible for calling the rcu_read_[un]lock()
- * functions.
- *
- */
-static struct netlbl_unlhsh_addr4 *netlbl_unlhsh_search_addr4(
-                                      __be32 addr,
-                                      const struct netlbl_unlhsh_iface *iface)
-{
-       struct netlbl_unlhsh_addr4 *iter;
-
-       list_for_each_entry_rcu(iter, &iface->addr4_list, list)
-               if (iter->valid && (addr & iter->mask) == iter->addr)
-                       return iter;
-
-       return NULL;
-}
-
-#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
-/**
- * netlbl_unlhsh_search_addr6 - Search for a matching IPv6 address entry
- * @addr: IPv6 address
- * @iface: the network interface entry
- *
- * Description:
- * Searches the IPv6 address list of the network interface specified by @iface.
- * If a matching address entry is found it is returned, otherwise NULL is
- * returned.  The caller is responsible for calling the rcu_read_[un]lock()
- * functions.
- *
- */
-static struct netlbl_unlhsh_addr6 *netlbl_unlhsh_search_addr6(
-                                      const struct in6_addr *addr,
-                                      const struct netlbl_unlhsh_iface *iface)
-{
-       struct netlbl_unlhsh_addr6 *iter;
-
-       list_for_each_entry_rcu(iter, &iface->addr6_list, list)
-               if (iter->valid &&
-                   ipv6_masked_addr_cmp(&iter->addr, &iter->mask, addr) == 0)
-               return iter;
-
-       return NULL;
-}
-#endif /* IPv6 */
-
-/**
  * netlbl_unlhsh_search_iface - Search for a matching interface entry
  * @ifindex: the network interface
  *
@@ -311,12 +259,12 @@ static struct netlbl_unlhsh_addr6 *netlbl_unlhsh_search_addr6(
 static struct netlbl_unlhsh_iface *netlbl_unlhsh_search_iface(int ifindex)
 {
        u32 bkt;
+       struct list_head *bkt_list;
        struct netlbl_unlhsh_iface *iter;
 
        bkt = netlbl_unlhsh_hash(ifindex);
-       list_for_each_entry_rcu(iter,
-                               &rcu_dereference(netlbl_unlhsh)->tbl[bkt],
-                               list)
+       bkt_list = &rcu_dereference(netlbl_unlhsh)->tbl[bkt];
+       list_for_each_entry_rcu(iter, bkt_list, list)
                if (iter->valid && iter->ifindex == ifindex)
                        return iter;
 
@@ -369,43 +317,25 @@ static int netlbl_unlhsh_add_addr4(struct netlbl_unlhsh_iface *iface,
                                   const struct in_addr *mask,
                                   u32 secid)
 {
+       int ret_val;
        struct netlbl_unlhsh_addr4 *entry;
-       struct netlbl_unlhsh_addr4 *iter;
 
        entry = kzalloc(sizeof(*entry), GFP_ATOMIC);
        if (entry == NULL)
                return -ENOMEM;
 
-       entry->addr = addr->s_addr & mask->s_addr;
-       entry->mask = mask->s_addr;
+       entry->list.addr = addr->s_addr & mask->s_addr;
+       entry->list.mask = mask->s_addr;
+       entry->list.valid = 1;
        entry->secid = secid;
-       entry->valid = 1;
-       INIT_RCU_HEAD(&entry->rcu);
 
        spin_lock(&netlbl_unlhsh_lock);
-       iter = netlbl_unlhsh_search_addr4(entry->addr, iface);
-       if (iter != NULL &&
-           iter->addr == addr->s_addr && iter->mask == mask->s_addr) {
-               spin_unlock(&netlbl_unlhsh_lock);
-               kfree(entry);
-               return -EEXIST;
-       }
-       /* in order to speed up address searches through the list (the common
-        * case) we need to keep the list in order based on the size of the
-        * address mask such that the entry with the widest mask (smallest
-        * numerical value) appears first in the list */
-       list_for_each_entry_rcu(iter, &iface->addr4_list, list)
-               if (iter->valid &&
-                   ntohl(entry->mask) > ntohl(iter->mask)) {
-                       __list_add_rcu(&entry->list,
-                                      iter->list.prev,
-                                      &iter->list);
-                       spin_unlock(&netlbl_unlhsh_lock);
-                       return 0;
-               }
-       list_add_tail_rcu(&entry->list, &iface->addr4_list);
+       ret_val = netlbl_af4list_add(&entry->list, &iface->addr4_list);
        spin_unlock(&netlbl_unlhsh_lock);
-       return 0;
+
+       if (ret_val != 0)
+               kfree(entry);
+       return ret_val;
 }
 
 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
@@ -428,47 +358,28 @@ static int netlbl_unlhsh_add_addr6(struct netlbl_unlhsh_iface *iface,
                                   const struct in6_addr *mask,
                                   u32 secid)
 {
+       int ret_val;
        struct netlbl_unlhsh_addr6 *entry;
-       struct netlbl_unlhsh_addr6 *iter;
 
        entry = kzalloc(sizeof(*entry), GFP_ATOMIC);
        if (entry == NULL)
                return -ENOMEM;
 
-       ipv6_addr_copy(&entry->addr, addr);
-       entry->addr.s6_addr32[0] &= mask->s6_addr32[0];
-       entry->addr.s6_addr32[1] &= mask->s6_addr32[1];
-       entry->addr.s6_addr32[2] &= mask->s6_addr32[2];
-       entry->addr.s6_addr32[3] &= mask->s6_addr32[3];
-       ipv6_addr_copy(&entry->mask, mask);
+       ipv6_addr_copy(&entry->list.addr, addr);
+       entry->list.addr.s6_addr32[0] &= mask->s6_addr32[0];
+       entry->list.addr.s6_addr32[1] &= mask->s6_addr32[1];
+       entry->list.addr.s6_addr32[2] &= mask->s6_addr32[2];
+       entry->list.addr.s6_addr32[3] &= mask->s6_addr32[3];
+       ipv6_addr_copy(&entry->list.mask, mask);
+       entry->list.valid = 1;
        entry->secid = secid;
-       entry->valid = 1;
-       INIT_RCU_HEAD(&entry->rcu);
 
        spin_lock(&netlbl_unlhsh_lock);
-       iter = netlbl_unlhsh_search_addr6(&entry->addr, iface);
-       if (iter != NULL &&
-           (ipv6_addr_equal(&iter->addr, addr) &&
-            ipv6_addr_equal(&iter->mask, mask))) {
-               spin_unlock(&netlbl_unlhsh_lock);
-               kfree(entry);
-               return -EEXIST;
-       }
-       /* in order to speed up address searches through the list (the common
-        * case) we need to keep the list in order based on the size of the
-        * address mask such that the entry with the widest mask (smallest
-        * numerical value) appears first in the list */
-       list_for_each_entry_rcu(iter, &iface->addr6_list, list)
-               if (iter->valid &&
-                   ipv6_addr_cmp(&entry->mask, &iter->mask) > 0) {
-                       __list_add_rcu(&entry->list,
-                                      iter->list.prev,
-                                      &iter->list);
-                       spin_unlock(&netlbl_unlhsh_lock);
-                       return 0;
-               }
-       list_add_tail_rcu(&entry->list, &iface->addr6_list);
+       ret_val = netlbl_af6list_add(&entry->list, &iface->addr6_list);
        spin_unlock(&netlbl_unlhsh_lock);
+
+       if (ret_val != 0)
+               kfree(entry);
        return 0;
 }
 #endif /* IPv6 */
@@ -497,7 +408,6 @@ static struct netlbl_unlhsh_iface *netlbl_unlhsh_add_iface(int ifindex)
        INIT_LIST_HEAD(&iface->addr4_list);
        INIT_LIST_HEAD(&iface->addr6_list);
        iface->valid = 1;
-       INIT_RCU_HEAD(&iface->rcu);
 
        spin_lock(&netlbl_unlhsh_lock);
        if (ifindex > 0) {
@@ -530,23 +440,28 @@ add_iface_failure:
  * @mask: address mask in network byte order
  * @addr_len: length of address/mask (4 for IPv4, 16 for IPv6)
  * @secid: LSM secid value for the entry
+ * @audit_info: NetLabel audit information
  *
  * Description:
  * Adds a new entry to the unlabeled connection hash table.  Returns zero on
  * success, negative values on failure.
  *
  */
-static int netlbl_unlhsh_add(struct net *net,
-                            const char *dev_name,
-                            const void *addr,
-                            const void *mask,
-                            u32 addr_len,
-                            u32 secid)
+int netlbl_unlhsh_add(struct net *net,
+                     const char *dev_name,
+                     const void *addr,
+                     const void *mask,
+                     u32 addr_len,
+                     u32 secid,
+                     struct netlbl_audit *audit_info)
 {
        int ret_val;
        int ifindex;
        struct net_device *dev;
        struct netlbl_unlhsh_iface *iface;
+       struct audit_buffer *audit_buf = NULL;
+       char *secctx = NULL;
+       u32 secctx_len;
 
        if (addr_len != sizeof(struct in_addr) &&
            addr_len != sizeof(struct in6_addr))
@@ -554,13 +469,12 @@ static int netlbl_unlhsh_add(struct net *net,
 
        rcu_read_lock();
        if (dev_name != NULL) {
-               dev = dev_get_by_name(net, dev_name);
+               dev = dev_get_by_name_rcu(net, dev_name);
                if (dev == NULL) {
                        ret_val = -ENODEV;
                        goto unlhsh_add_return;
                }
                ifindex = dev->ifindex;
-               dev_put(dev);
                iface = netlbl_unlhsh_search_iface(ifindex);
        } else {
                ifindex = 0;
@@ -573,14 +487,35 @@ static int netlbl_unlhsh_add(struct net *net,
                        goto unlhsh_add_return;
                }
        }
+       audit_buf = netlbl_audit_start_common(AUDIT_MAC_UNLBL_STCADD,
+                                             audit_info);
        switch (addr_len) {
-       case sizeof(struct in_addr):
-               ret_val = netlbl_unlhsh_add_addr4(iface, addr, mask, secid);
+       case sizeof(struct in_addr): {
+               struct in_addr *addr4, *mask4;
+
+               addr4 = (struct in_addr *)addr;
+               mask4 = (struct in_addr *)mask;
+               ret_val = netlbl_unlhsh_add_addr4(iface, addr4, mask4, secid);
+               if (audit_buf != NULL)
+                       netlbl_af4list_audit_addr(audit_buf, 1,
+                                                 dev_name,
+                                                 addr4->s_addr,
+                                                 mask4->s_addr);
                break;
+       }
 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
-       case sizeof(struct in6_addr):
-               ret_val = netlbl_unlhsh_add_addr6(iface, addr, mask, secid);
+       case sizeof(struct in6_addr): {
+               struct in6_addr *addr6, *mask6;
+
+               addr6 = (struct in6_addr *)addr;
+               mask6 = (struct in6_addr *)mask;
+               ret_val = netlbl_unlhsh_add_addr6(iface, addr6, mask6, secid);
+               if (audit_buf != NULL)
+                       netlbl_af6list_audit_addr(audit_buf, 1,
+                                                 dev_name,
+                                                 addr6, mask6);
                break;
+       }
 #endif /* IPv6 */
        default:
                ret_val = -EINVAL;
@@ -590,14 +525,26 @@ static int netlbl_unlhsh_add(struct net *net,
 
 unlhsh_add_return:
        rcu_read_unlock();
+       if (audit_buf != NULL) {
+               if (security_secid_to_secctx(secid,
+                                            &secctx,
+                                            &secctx_len) == 0) {
+                       audit_log_format(audit_buf, " sec_obj=%s", secctx);
+                       security_release_secctx(secctx, secctx_len);
+               }
+               audit_log_format(audit_buf, " res=%u", ret_val == 0 ? 1 : 0);
+               audit_log_end(audit_buf);
+       }
        return ret_val;
 }
 
 /**
  * netlbl_unlhsh_remove_addr4 - Remove an IPv4 address entry
+ * @net: network namespace
  * @iface: interface entry
  * @addr: IP address
  * @mask: IP address mask
+ * @audit_info: NetLabel audit information
  *
  * Description:
  * Remove an IP address entry from the unlabeled connection hash table.
@@ -605,34 +552,62 @@ unlhsh_add_return:
  * responsible for calling the rcu_read_[un]lock() functions.
  *
  */
-static int netlbl_unlhsh_remove_addr4(struct netlbl_unlhsh_iface *iface,
+static int netlbl_unlhsh_remove_addr4(struct net *net,
+                                     struct netlbl_unlhsh_iface *iface,
                                      const struct in_addr *addr,
-                                     const struct in_addr *mask)
+                                     const struct in_addr *mask,
+                                     struct netlbl_audit *audit_info)
 {
-       int ret_val = -ENOENT;
+       struct netlbl_af4list *list_entry;
        struct netlbl_unlhsh_addr4 *entry;
+       struct audit_buffer *audit_buf;
+       struct net_device *dev;
+       char *secctx;
+       u32 secctx_len;
 
        spin_lock(&netlbl_unlhsh_lock);
-       entry = netlbl_unlhsh_search_addr4(addr->s_addr, iface);
-       if (entry != NULL &&
-           entry->addr == addr->s_addr && entry->mask == mask->s_addr) {
-               entry->valid = 0;
-               list_del_rcu(&entry->list);
-               ret_val = 0;
-       }
+       list_entry = netlbl_af4list_remove(addr->s_addr, mask->s_addr,
+                                          &iface->addr4_list);
        spin_unlock(&netlbl_unlhsh_lock);
+       if (list_entry != NULL)
+               entry = netlbl_unlhsh_addr4_entry(list_entry);
+       else
+               entry = NULL;
 
-       if (ret_val == 0)
-               call_rcu(&entry->rcu, netlbl_unlhsh_free_addr4);
-       return ret_val;
+       audit_buf = netlbl_audit_start_common(AUDIT_MAC_UNLBL_STCDEL,
+                                             audit_info);
+       if (audit_buf != NULL) {
+               dev = dev_get_by_index(net, iface->ifindex);
+               netlbl_af4list_audit_addr(audit_buf, 1,
+                                         (dev != NULL ? dev->name : NULL),
+                                         addr->s_addr, mask->s_addr);
+               if (dev != NULL)
+                       dev_put(dev);
+               if (entry != NULL &&
+                   security_secid_to_secctx(entry->secid,
+                                            &secctx, &secctx_len) == 0) {
+                       audit_log_format(audit_buf, " sec_obj=%s", secctx);
+                       security_release_secctx(secctx, secctx_len);
+               }
+               audit_log_format(audit_buf, " res=%u", entry != NULL ? 1 : 0);
+               audit_log_end(audit_buf);
+       }
+
+       if (entry == NULL)
+               return -ENOENT;
+
+       call_rcu(&entry->rcu, netlbl_unlhsh_free_addr4);
+       return 0;
 }
 
 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
 /**
  * netlbl_unlhsh_remove_addr6 - Remove an IPv6 address entry
+ * @net: network namespace
  * @iface: interface entry
  * @addr: IP address
  * @mask: IP address mask
+ * @audit_info: NetLabel audit information
  *
  * Description:
  * Remove an IP address entry from the unlabeled connection hash table.
@@ -640,27 +615,51 @@ static int netlbl_unlhsh_remove_addr4(struct netlbl_unlhsh_iface *iface,
  * responsible for calling the rcu_read_[un]lock() functions.
  *
  */
-static int netlbl_unlhsh_remove_addr6(struct netlbl_unlhsh_iface *iface,
+static int netlbl_unlhsh_remove_addr6(struct net *net,
+                                     struct netlbl_unlhsh_iface *iface,
                                      const struct in6_addr *addr,
-                                     const struct in6_addr *mask)
+                                     const struct in6_addr *mask,
+                                     struct netlbl_audit *audit_info)
 {
-       int ret_val = -ENOENT;
+       struct netlbl_af6list *list_entry;
        struct netlbl_unlhsh_addr6 *entry;
+       struct audit_buffer *audit_buf;
+       struct net_device *dev;
+       char *secctx;
+       u32 secctx_len;
 
        spin_lock(&netlbl_unlhsh_lock);
-       entry = netlbl_unlhsh_search_addr6(addr, iface);
-       if (entry != NULL &&
-           (ipv6_addr_equal(&entry->addr, addr) &&
-            ipv6_addr_equal(&entry->mask, mask))) {
-               entry->valid = 0;
-               list_del_rcu(&entry->list);
-               ret_val = 0;
-       }
+       list_entry = netlbl_af6list_remove(addr, mask, &iface->addr6_list);
        spin_unlock(&netlbl_unlhsh_lock);
+       if (list_entry != NULL)
+               entry = netlbl_unlhsh_addr6_entry(list_entry);
+       else
+               entry = NULL;
 
-       if (ret_val == 0)
-               call_rcu(&entry->rcu, netlbl_unlhsh_free_addr6);
-       return ret_val;
+       audit_buf = netlbl_audit_start_common(AUDIT_MAC_UNLBL_STCDEL,
+                                             audit_info);
+       if (audit_buf != NULL) {
+               dev = dev_get_by_index(net, iface->ifindex);
+               netlbl_af6list_audit_addr(audit_buf, 1,
+                                         (dev != NULL ? dev->name : NULL),
+                                         addr, mask);
+               if (dev != NULL)
+                       dev_put(dev);
+               if (entry != NULL &&
+                   security_secid_to_secctx(entry->secid,
+                                            &secctx, &secctx_len) == 0) {
+                       audit_log_format(audit_buf, " sec_obj=%s", secctx);
+                       security_release_secctx(secctx, secctx_len);
+               }
+               audit_log_format(audit_buf, " res=%u", entry != NULL ? 1 : 0);
+               audit_log_end(audit_buf);
+       }
+
+       if (entry == NULL)
+               return -ENOENT;
+
+       call_rcu(&entry->rcu, netlbl_unlhsh_free_addr6);
+       return 0;
 }
 #endif /* IPv6 */
 
@@ -676,16 +675,18 @@ static int netlbl_unlhsh_remove_addr6(struct netlbl_unlhsh_iface *iface,
  */
 static void netlbl_unlhsh_condremove_iface(struct netlbl_unlhsh_iface *iface)
 {
-       struct netlbl_unlhsh_addr4 *iter4;
-       struct netlbl_unlhsh_addr6 *iter6;
+       struct netlbl_af4list *iter4;
+#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
+       struct netlbl_af6list *iter6;
+#endif /* IPv6 */
 
        spin_lock(&netlbl_unlhsh_lock);
-       list_for_each_entry_rcu(iter4, &iface->addr4_list, list)
-               if (iter4->valid)
-                       goto unlhsh_condremove_failure;
-       list_for_each_entry_rcu(iter6, &iface->addr6_list, list)
-               if (iter6->valid)
-                       goto unlhsh_condremove_failure;
+       netlbl_af4list_foreach_rcu(iter4, &iface->addr4_list)
+               goto unlhsh_condremove_failure;
+#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
+       netlbl_af6list_foreach_rcu(iter6, &iface->addr6_list)
+               goto unlhsh_condremove_failure;
+#endif /* IPv6 */
        iface->valid = 0;
        if (iface->ifindex > 0)
                list_del_rcu(&iface->list);
@@ -708,17 +709,19 @@ unlhsh_condremove_failure:
  * @addr: IP address in network byte order
  * @mask: address mask in network byte order
  * @addr_len: length of address/mask (4 for IPv4, 16 for IPv6)
+ * @audit_info: NetLabel audit information
  *
  * Description:
  * Removes and existing entry from the unlabeled connection hash table.
  * Returns zero on success, negative values on failure.
  *
  */
-static int netlbl_unlhsh_remove(struct net *net,
-                               const char *dev_name,
-                               const void *addr,
-                               const void *mask,
-                               u32 addr_len)
+int netlbl_unlhsh_remove(struct net *net,
+                        const char *dev_name,
+                        const void *addr,
+                        const void *mask,
+                        u32 addr_len,
+                        struct netlbl_audit *audit_info)
 {
        int ret_val;
        struct net_device *dev;
@@ -730,13 +733,12 @@ static int netlbl_unlhsh_remove(struct net *net,
 
        rcu_read_lock();
        if (dev_name != NULL) {
-               dev = dev_get_by_name(net, dev_name);
+               dev = dev_get_by_name_rcu(net, dev_name);
                if (dev == NULL) {
                        ret_val = -ENODEV;
                        goto unlhsh_remove_return;
                }
                iface = netlbl_unlhsh_search_iface(dev->ifindex);
-               dev_put(dev);
        } else
                iface = rcu_dereference(netlbl_unlhsh_def);
        if (iface == NULL) {
@@ -745,11 +747,15 @@ static int netlbl_unlhsh_remove(struct net *net,
        }
        switch (addr_len) {
        case sizeof(struct in_addr):
-               ret_val = netlbl_unlhsh_remove_addr4(iface, addr, mask);
+               ret_val = netlbl_unlhsh_remove_addr4(net,
+                                                    iface, addr, mask,
+                                                    audit_info);
                break;
 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
        case sizeof(struct in6_addr):
-               ret_val = netlbl_unlhsh_remove_addr6(iface, addr, mask);
+               ret_val = netlbl_unlhsh_remove_addr6(net,
+                                                    iface, addr, mask,
+                                                    audit_info);
                break;
 #endif /* IPv6 */
        default:
@@ -788,7 +794,7 @@ static int netlbl_unlhsh_netdev_handler(struct notifier_block *this,
        struct net_device *dev = ptr;
        struct netlbl_unlhsh_iface *iface = NULL;
 
-       if (dev->nd_net != &init_net)
+       if (!net_eq(dev_net(dev), &init_net))
                return NOTIFY_DONE;
 
        /* XXX - should this be a check for NETDEV_DOWN or _UNREGISTER? */
@@ -941,11 +947,7 @@ static int netlbl_unlabel_list(struct sk_buff *skb, struct genl_info *info)
                goto list_failure;
 
        genlmsg_end(ans_skb, data);
-
-       ret_val = genlmsg_reply(ans_skb, info);
-       if (ret_val != 0)
-               goto list_failure;
-       return 0;
+       return genlmsg_reply(ans_skb, info);
 
 list_failure:
        kfree_skb(ans_skb);
@@ -972,6 +974,7 @@ static int netlbl_unlabel_staticadd(struct sk_buff *skb,
        void *mask;
        u32 addr_len;
        u32 secid;
+       struct netlbl_audit audit_info;
 
        /* Don't allow users to add both IPv4 and IPv6 addresses for a
         * single entry.  However, allow users to create two entries, one each
@@ -985,6 +988,8 @@ static int netlbl_unlabel_staticadd(struct sk_buff *skb,
               !info->attrs[NLBL_UNLABEL_A_IPV6MASK])))
                return -EINVAL;
 
+       netlbl_netlink_auditinfo(skb, &audit_info);
+
        ret_val = netlbl_unlabel_addrinfo_get(info, &addr, &mask, &addr_len);
        if (ret_val != 0)
                return ret_val;
@@ -997,7 +1002,8 @@ static int netlbl_unlabel_staticadd(struct sk_buff *skb,
                return ret_val;
 
        return netlbl_unlhsh_add(&init_net,
-                                dev_name, addr, mask, addr_len, secid);
+                                dev_name, addr, mask, addr_len, secid,
+                                &audit_info);
 }
 
 /**
@@ -1019,6 +1025,7 @@ static int netlbl_unlabel_staticadddef(struct sk_buff *skb,
        void *mask;
        u32 addr_len;
        u32 secid;
+       struct netlbl_audit audit_info;
 
        /* Don't allow users to add both IPv4 and IPv6 addresses for a
         * single entry.  However, allow users to create two entries, one each
@@ -1031,6 +1038,8 @@ static int netlbl_unlabel_staticadddef(struct sk_buff *skb,
               !info->attrs[NLBL_UNLABEL_A_IPV6MASK])))
                return -EINVAL;
 
+       netlbl_netlink_auditinfo(skb, &audit_info);
+
        ret_val = netlbl_unlabel_addrinfo_get(info, &addr, &mask, &addr_len);
        if (ret_val != 0)
                return ret_val;
@@ -1041,7 +1050,9 @@ static int netlbl_unlabel_staticadddef(struct sk_buff *skb,
        if (ret_val != 0)
                return ret_val;
 
-       return netlbl_unlhsh_add(&init_net, NULL, addr, mask, addr_len, secid);
+       return netlbl_unlhsh_add(&init_net,
+                                NULL, addr, mask, addr_len, secid,
+                                &audit_info);
 }
 
 /**
@@ -1063,6 +1074,7 @@ static int netlbl_unlabel_staticremove(struct sk_buff *skb,
        void *addr;
        void *mask;
        u32 addr_len;
+       struct netlbl_audit audit_info;
 
        /* See the note in netlbl_unlabel_staticadd() about not allowing both
         * IPv4 and IPv6 in the same entry. */
@@ -1073,12 +1085,16 @@ static int netlbl_unlabel_staticremove(struct sk_buff *skb,
               !info->attrs[NLBL_UNLABEL_A_IPV6MASK])))
                return -EINVAL;
 
+       netlbl_netlink_auditinfo(skb, &audit_info);
+
        ret_val = netlbl_unlabel_addrinfo_get(info, &addr, &mask, &addr_len);
        if (ret_val != 0)
                return ret_val;
        dev_name = nla_data(info->attrs[NLBL_UNLABEL_A_IFACE]);
 
-       return netlbl_unlhsh_remove(&init_net, dev_name, addr, mask, addr_len);
+       return netlbl_unlhsh_remove(&init_net,
+                                   dev_name, addr, mask, addr_len,
+                                   &audit_info);
 }
 
 /**
@@ -1099,6 +1115,7 @@ static int netlbl_unlabel_staticremovedef(struct sk_buff *skb,
        void *addr;
        void *mask;
        u32 addr_len;
+       struct netlbl_audit audit_info;
 
        /* See the note in netlbl_unlabel_staticadd() about not allowing both
         * IPv4 and IPv6 in the same entry. */
@@ -1108,11 +1125,15 @@ static int netlbl_unlabel_staticremovedef(struct sk_buff *skb,
               !info->attrs[NLBL_UNLABEL_A_IPV6MASK])))
                return -EINVAL;
 
+       netlbl_netlink_auditinfo(skb, &audit_info);
+
        ret_val = netlbl_unlabel_addrinfo_get(info, &addr, &mask, &addr_len);
        if (ret_val != 0)
                return ret_val;
 
-       return netlbl_unlhsh_remove(&init_net, NULL, addr, mask, addr_len);
+       return netlbl_unlhsh_remove(&init_net,
+                                   NULL, addr, mask, addr_len,
+                                   &audit_info);
 }
 
 
@@ -1154,6 +1175,10 @@ static int netlbl_unlabel_staticlist_gen(u32 cmd,
 
        if (iface->ifindex > 0) {
                dev = dev_get_by_index(&init_net, iface->ifindex);
+               if (!dev) {
+                       ret_val = -ENODEV;
+                       goto list_cb_failure;
+               }
                ret_val = nla_put_string(cb_arg->skb,
                                         NLBL_UNLABEL_A_IFACE, dev->name);
                dev_put(dev);
@@ -1164,7 +1189,7 @@ static int netlbl_unlabel_staticlist_gen(u32 cmd,
        if (addr4) {
                struct in_addr addr_struct;
 
-               addr_struct.s_addr = addr4->addr;
+               addr_struct.s_addr = addr4->list.addr;
                ret_val = nla_put(cb_arg->skb,
                                  NLBL_UNLABEL_A_IPV4ADDR,
                                  sizeof(struct in_addr),
@@ -1172,7 +1197,7 @@ static int netlbl_unlabel_staticlist_gen(u32 cmd,
                if (ret_val != 0)
                        goto list_cb_failure;
 
-               addr_struct.s_addr = addr4->mask;
+               addr_struct.s_addr = addr4->list.mask;
                ret_val = nla_put(cb_arg->skb,
                                  NLBL_UNLABEL_A_IPV4MASK,
                                  sizeof(struct in_addr),
@@ -1185,14 +1210,14 @@ static int netlbl_unlabel_staticlist_gen(u32 cmd,
                ret_val = nla_put(cb_arg->skb,
                                  NLBL_UNLABEL_A_IPV6ADDR,
                                  sizeof(struct in6_addr),
-                                 &addr6->addr);
+                                 &addr6->list.addr);
                if (ret_val != 0)
                        goto list_cb_failure;
 
                ret_val = nla_put(cb_arg->skb,
                                  NLBL_UNLABEL_A_IPV6MASK,
                                  sizeof(struct in6_addr),
-                                 &addr6->mask);
+                                 &addr6->list.mask);
                if (ret_val != 0)
                        goto list_cb_failure;
 
@@ -1240,8 +1265,11 @@ static int netlbl_unlabel_staticlist(struct sk_buff *skb,
        u32 iter_bkt;
        u32 iter_chain = 0, iter_addr4 = 0, iter_addr6 = 0;
        struct netlbl_unlhsh_iface *iface;
-       struct netlbl_unlhsh_addr4 *addr4;
-       struct netlbl_unlhsh_addr6 *addr6;
+       struct list_head *iter_list;
+       struct netlbl_af4list *addr4;
+#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
+       struct netlbl_af6list *addr6;
+#endif
 
        cb_arg.nl_cb = cb;
        cb_arg.skb = skb;
@@ -1251,44 +1279,43 @@ static int netlbl_unlabel_staticlist(struct sk_buff *skb,
        for (iter_bkt = skip_bkt;
             iter_bkt < rcu_dereference(netlbl_unlhsh)->size;
             iter_bkt++, iter_chain = 0, iter_addr4 = 0, iter_addr6 = 0) {
-               list_for_each_entry_rcu(iface,
-                               &rcu_dereference(netlbl_unlhsh)->tbl[iter_bkt],
-                               list) {
+               iter_list = &rcu_dereference(netlbl_unlhsh)->tbl[iter_bkt];
+               list_for_each_entry_rcu(iface, iter_list, list) {
                        if (!iface->valid ||
                            iter_chain++ < skip_chain)
                                continue;
-                       list_for_each_entry_rcu(addr4,
-                                               &iface->addr4_list,
-                                               list) {
-                               if (!addr4->valid || iter_addr4++ < skip_addr4)
+                       netlbl_af4list_foreach_rcu(addr4,
+                                                  &iface->addr4_list) {
+                               if (iter_addr4++ < skip_addr4)
                                        continue;
                                if (netlbl_unlabel_staticlist_gen(
-                                                    NLBL_UNLABEL_C_STATICLIST,
-                                                    iface,
-                                                    addr4,
-                                                    NULL,
-                                                    &cb_arg) < 0) {
+                                             NLBL_UNLABEL_C_STATICLIST,
+                                             iface,
+                                             netlbl_unlhsh_addr4_entry(addr4),
+                                             NULL,
+                                             &cb_arg) < 0) {
                                        iter_addr4--;
                                        iter_chain--;
                                        goto unlabel_staticlist_return;
                                }
                        }
-                       list_for_each_entry_rcu(addr6,
-                                               &iface->addr6_list,
-                                               list) {
-                               if (!addr6->valid || iter_addr6++ < skip_addr6)
+#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
+                       netlbl_af6list_foreach_rcu(addr6,
+                                                  &iface->addr6_list) {
+                               if (iter_addr6++ < skip_addr6)
                                        continue;
                                if (netlbl_unlabel_staticlist_gen(
-                                                    NLBL_UNLABEL_C_STATICLIST,
-                                                    iface,
-                                                    NULL,
-                                                    addr6,
-                                                    &cb_arg) < 0) {
+                                             NLBL_UNLABEL_C_STATICLIST,
+                                             iface,
+                                             NULL,
+                                             netlbl_unlhsh_addr6_entry(addr6),
+                                             &cb_arg) < 0) {
                                        iter_addr6--;
                                        iter_chain--;
                                        goto unlabel_staticlist_return;
                                }
                        }
+#endif /* IPv6 */
                }
        }
 
@@ -1319,9 +1346,12 @@ static int netlbl_unlabel_staticlistdef(struct sk_buff *skb,
        struct netlbl_unlhsh_iface *iface;
        u32 skip_addr4 = cb->args[0];
        u32 skip_addr6 = cb->args[1];
-       u32 iter_addr4 = 0, iter_addr6 = 0;
-       struct netlbl_unlhsh_addr4 *addr4;
-       struct netlbl_unlhsh_addr6 *addr6;
+       u32 iter_addr4 = 0;
+       struct netlbl_af4list *addr4;
+#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
+       u32 iter_addr6 = 0;
+       struct netlbl_af6list *addr6;
+#endif
 
        cb_arg.nl_cb = cb;
        cb_arg.skb = skb;
@@ -1332,30 +1362,32 @@ static int netlbl_unlabel_staticlistdef(struct sk_buff *skb,
        if (iface == NULL || !iface->valid)
                goto unlabel_staticlistdef_return;
 
-       list_for_each_entry_rcu(addr4, &iface->addr4_list, list) {
-               if (!addr4->valid || iter_addr4++ < skip_addr4)
+       netlbl_af4list_foreach_rcu(addr4, &iface->addr4_list) {
+               if (iter_addr4++ < skip_addr4)
                        continue;
                if (netlbl_unlabel_staticlist_gen(NLBL_UNLABEL_C_STATICLISTDEF,
-                                          iface,
-                                          addr4,
-                                          NULL,
-                                          &cb_arg) < 0) {
+                                             iface,
+                                             netlbl_unlhsh_addr4_entry(addr4),
+                                             NULL,
+                                             &cb_arg) < 0) {
                        iter_addr4--;
                        goto unlabel_staticlistdef_return;
                }
        }
-       list_for_each_entry_rcu(addr6, &iface->addr6_list, list) {
-               if (addr6->valid || iter_addr6++ < skip_addr6)
+#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
+       netlbl_af6list_foreach_rcu(addr6, &iface->addr6_list) {
+               if (iter_addr6++ < skip_addr6)
                        continue;
                if (netlbl_unlabel_staticlist_gen(NLBL_UNLABEL_C_STATICLISTDEF,
-                                          iface,
-                                          NULL,
-                                          addr6,
-                                          &cb_arg) < 0) {
+                                             iface,
+                                             NULL,
+                                             netlbl_unlhsh_addr6_entry(addr6),
+                                             &cb_arg) < 0) {
                        iter_addr6--;
                        goto unlabel_staticlistdef_return;
                }
        }
+#endif /* IPv6 */
 
 unlabel_staticlistdef_return:
        rcu_read_unlock();
@@ -1368,68 +1400,63 @@ unlabel_staticlistdef_return:
  * NetLabel Generic NETLINK Command Definitions
  */
 
-static struct genl_ops netlbl_unlabel_genl_c_staticadd = {
+static struct genl_ops netlbl_unlabel_genl_ops[] = {
+       {
        .cmd = NLBL_UNLABEL_C_STATICADD,
        .flags = GENL_ADMIN_PERM,
        .policy = netlbl_unlabel_genl_policy,
        .doit = netlbl_unlabel_staticadd,
        .dumpit = NULL,
-};
-
-static struct genl_ops netlbl_unlabel_genl_c_staticremove = {
+       },
+       {
        .cmd = NLBL_UNLABEL_C_STATICREMOVE,
        .flags = GENL_ADMIN_PERM,
        .policy = netlbl_unlabel_genl_policy,
        .doit = netlbl_unlabel_staticremove,
        .dumpit = NULL,
-};
-
-static struct genl_ops netlbl_unlabel_genl_c_staticlist = {
+       },
+       {
        .cmd = NLBL_UNLABEL_C_STATICLIST,
        .flags = 0,
        .policy = netlbl_unlabel_genl_policy,
        .doit = NULL,
        .dumpit = netlbl_unlabel_staticlist,
-};
-
-static struct genl_ops netlbl_unlabel_genl_c_staticadddef = {
+       },
+       {
        .cmd = NLBL_UNLABEL_C_STATICADDDEF,
        .flags = GENL_ADMIN_PERM,
        .policy = netlbl_unlabel_genl_policy,
        .doit = netlbl_unlabel_staticadddef,
        .dumpit = NULL,
-};
-
-static struct genl_ops netlbl_unlabel_genl_c_staticremovedef = {
+       },
+       {
        .cmd = NLBL_UNLABEL_C_STATICREMOVEDEF,
        .flags = GENL_ADMIN_PERM,
        .policy = netlbl_unlabel_genl_policy,
        .doit = netlbl_unlabel_staticremovedef,
        .dumpit = NULL,
-};
-
-static struct genl_ops netlbl_unlabel_genl_c_staticlistdef = {
+       },
+       {
        .cmd = NLBL_UNLABEL_C_STATICLISTDEF,
        .flags = 0,
        .policy = netlbl_unlabel_genl_policy,
        .doit = NULL,
        .dumpit = netlbl_unlabel_staticlistdef,
-};
-
-static struct genl_ops netlbl_unlabel_genl_c_accept = {
+       },
+       {
        .cmd = NLBL_UNLABEL_C_ACCEPT,
        .flags = GENL_ADMIN_PERM,
        .policy = netlbl_unlabel_genl_policy,
        .doit = netlbl_unlabel_accept,
        .dumpit = NULL,
-};
-
-static struct genl_ops netlbl_unlabel_genl_c_list = {
+       },
+       {
        .cmd = NLBL_UNLABEL_C_LIST,
        .flags = 0,
        .policy = netlbl_unlabel_genl_policy,
        .doit = netlbl_unlabel_list,
        .dumpit = NULL,
+       },
 };
 
 /*
@@ -1444,55 +1471,10 @@ static struct genl_ops netlbl_unlabel_genl_c_list = {
  * mechanism.  Returns zero on success, negative values on failure.
  *
  */
-int netlbl_unlabel_genl_init(void)
+int __init netlbl_unlabel_genl_init(void)
 {
-       int ret_val;
-
-       ret_val = genl_register_family(&netlbl_unlabel_gnl_family);
-       if (ret_val != 0)
-               return ret_val;
-
-       ret_val = genl_register_ops(&netlbl_unlabel_gnl_family,
-                                   &netlbl_unlabel_genl_c_staticadd);
-       if (ret_val != 0)
-               return ret_val;
-
-       ret_val = genl_register_ops(&netlbl_unlabel_gnl_family,
-                                   &netlbl_unlabel_genl_c_staticremove);
-       if (ret_val != 0)
-               return ret_val;
-
-       ret_val = genl_register_ops(&netlbl_unlabel_gnl_family,
-                                   &netlbl_unlabel_genl_c_staticlist);
-       if (ret_val != 0)
-               return ret_val;
-
-       ret_val = genl_register_ops(&netlbl_unlabel_gnl_family,
-                                   &netlbl_unlabel_genl_c_staticadddef);
-       if (ret_val != 0)
-               return ret_val;
-
-       ret_val = genl_register_ops(&netlbl_unlabel_gnl_family,
-                                   &netlbl_unlabel_genl_c_staticremovedef);
-       if (ret_val != 0)
-               return ret_val;
-
-       ret_val = genl_register_ops(&netlbl_unlabel_gnl_family,
-                                   &netlbl_unlabel_genl_c_staticlistdef);
-       if (ret_val != 0)
-               return ret_val;
-
-       ret_val = genl_register_ops(&netlbl_unlabel_gnl_family,
-                                   &netlbl_unlabel_genl_c_accept);
-       if (ret_val != 0)
-               return ret_val;
-
-       ret_val = genl_register_ops(&netlbl_unlabel_gnl_family,
-                                   &netlbl_unlabel_genl_c_list);
-       if (ret_val != 0)
-               return ret_val;
-
-       return 0;
+       return genl_register_family_with_ops(&netlbl_unlabel_gnl_family,
+               netlbl_unlabel_genl_ops, ARRAY_SIZE(netlbl_unlabel_genl_ops));
 }
 
 /*
@@ -1514,7 +1496,7 @@ static struct notifier_block netlbl_unlhsh_netdev_notifier = {
  * non-zero values on error.
  *
  */
-int netlbl_unlabel_init(u32 size)
+int __init netlbl_unlabel_init(u32 size)
 {
        u32 iter;
        struct netlbl_unlhsh_tbl *hsh_tbl;
@@ -1562,32 +1544,38 @@ int netlbl_unlabel_getattr(const struct sk_buff *skb,
                           u16 family,
                           struct netlbl_lsm_secattr *secattr)
 {
-       struct iphdr *hdr4;
-       struct ipv6hdr *hdr6;
-       struct netlbl_unlhsh_addr4 *addr4;
-       struct netlbl_unlhsh_addr6 *addr6;
        struct netlbl_unlhsh_iface *iface;
 
        rcu_read_lock();
-       iface = netlbl_unlhsh_search_iface_def(skb->iif);
+       iface = netlbl_unlhsh_search_iface_def(skb->skb_iif);
        if (iface == NULL)
                goto unlabel_getattr_nolabel;
        switch (family) {
-       case PF_INET:
+       case PF_INET: {
+               struct iphdr *hdr4;
+               struct netlbl_af4list *addr4;
+
                hdr4 = ip_hdr(skb);
-               addr4 = netlbl_unlhsh_search_addr4(hdr4->saddr, iface);
+               addr4 = netlbl_af4list_search(hdr4->saddr,
+                                             &iface->addr4_list);
                if (addr4 == NULL)
                        goto unlabel_getattr_nolabel;
-               secattr->attr.secid = addr4->secid;
+               secattr->attr.secid = netlbl_unlhsh_addr4_entry(addr4)->secid;
                break;
+       }
 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
-       case PF_INET6:
+       case PF_INET6: {
+               struct ipv6hdr *hdr6;
+               struct netlbl_af6list *addr6;
+
                hdr6 = ipv6_hdr(skb);
-               addr6 = netlbl_unlhsh_search_addr6(&hdr6->saddr, iface);
+               addr6 = netlbl_af6list_search(&hdr6->saddr,
+                                             &iface->addr6_list);
                if (addr6 == NULL)
                        goto unlabel_getattr_nolabel;
-               secattr->attr.secid = addr6->secid;
+               secattr->attr.secid = netlbl_unlhsh_addr6_entry(addr6)->secid;
                break;
+       }
 #endif /* IPv6 */
        default:
                goto unlabel_getattr_nolabel;
@@ -1614,7 +1602,7 @@ unlabel_getattr_nolabel:
  * and to send unlabeled network traffic by default.
  *
  */
-int netlbl_unlabel_defconf(void)
+int __init netlbl_unlabel_defconf(void)
 {
        int ret_val;
        struct netlbl_dom_map *entry;
@@ -1625,6 +1613,7 @@ int netlbl_unlabel_defconf(void)
         * messages so don't worry to much about these values. */
        security_task_getsecid(current, &audit_info.secid);
        audit_info.loginuid = 0;
+       audit_info.sessionid = 0;
 
        entry = kzalloc(sizeof(*entry), GFP_KERNEL);
        if (entry == NULL)