ALSA: usb-audio: add support for Akai MPD16
[safe/jmp/linux-2.6] / net / netlabel / netlabel_cipso_v4.c
index 73e0ff4..5f14c84 100644 (file)
 #include <linux/string.h>
 #include <linux/skbuff.h>
 #include <linux/audit.h>
+#include <linux/slab.h>
 #include <net/sock.h>
 #include <net/netlink.h>
 #include <net/genetlink.h>
 #include <net/netlabel.h>
 #include <net/cipso_ipv4.h>
+#include <asm/atomic.h>
 
 #include "netlabel_user.h"
 #include "netlabel_cipso_v4.h"
+#include "netlabel_mgmt.h"
+#include "netlabel_domainhash.h"
 
 /* Argument struct for cipso_v4_doi_walk() */
 struct netlbl_cipsov4_doiwalk_arg {
@@ -49,6 +53,12 @@ struct netlbl_cipsov4_doiwalk_arg {
        u32 seq;
 };
 
+/* Argument struct for netlbl_domhsh_walk() */
+struct netlbl_domhsh_walk_arg {
+       struct netlbl_audit *audit_info;
+       u32 doi;
+};
+
 /* NetLabel Generic NETLINK CIPSOv4 family */
 static struct genl_family netlbl_cipsov4_gnl_family = {
        .id = GENL_ID_GENERATE,
@@ -59,7 +69,7 @@ static struct genl_family netlbl_cipsov4_gnl_family = {
 };
 
 /* NetLabel Netlink attribute policy */
-static struct nla_policy netlbl_cipsov4_genl_policy[NLBL_CIPSOV4_A_MAX + 1] = {
+static const struct nla_policy netlbl_cipsov4_genl_policy[NLBL_CIPSOV4_A_MAX + 1] = {
        [NLBL_CIPSOV4_A_DOI] = { .type = NLA_U32 },
        [NLBL_CIPSOV4_A_MTYPE] = { .type = NLA_U32 },
        [NLBL_CIPSOV4_A_TAG] = { .type = NLA_U8 },
@@ -79,32 +89,6 @@ static struct nla_policy netlbl_cipsov4_genl_policy[NLBL_CIPSOV4_A_MAX + 1] = {
  */
 
 /**
- * netlbl_cipsov4_doi_free - Frees a CIPSO V4 DOI definition
- * @entry: the entry's RCU field
- *
- * Description:
- * This function is designed to be used as a callback to the call_rcu()
- * function so that the memory allocated to the DOI definition can be released
- * safely.
- *
- */
-static void netlbl_cipsov4_doi_free(struct rcu_head *entry)
-{
-       struct cipso_v4_doi *ptr;
-
-       ptr = container_of(entry, struct cipso_v4_doi, rcu);
-       switch (ptr->type) {
-       case CIPSO_V4_MAP_STD:
-               kfree(ptr->map.std->lvl.cipso);
-               kfree(ptr->map.std->lvl.local);
-               kfree(ptr->map.std->cat.cipso);
-               kfree(ptr->map.std->cat.local);
-               break;
-       }
-       kfree(ptr);
-}
-
-/**
  * netlbl_cipsov4_add_common - Parse the common sections of a ADD message
  * @info: the Generic NETLINK info block
  * @doi_def: the CIPSO V4 DOI definition
@@ -129,7 +113,7 @@ static int netlbl_cipsov4_add_common(struct genl_info *info,
                return -EINVAL;
 
        nla_for_each_nested(nla, info->attrs[NLBL_CIPSOV4_A_TAGLST], nla_rem)
-               if (nla->nla_type == NLBL_CIPSOV4_A_TAG) {
+               if (nla_type(nla) == NLBL_CIPSOV4_A_TAG) {
                        if (iter >= CIPSO_V4_TAG_MAXCNT)
                                return -EINVAL;
                        doi_def->tags[iter++] = nla_get_u8(nla);
@@ -147,14 +131,16 @@ static int netlbl_cipsov4_add_common(struct genl_info *info,
 /**
  * netlbl_cipsov4_add_std - Adds a CIPSO V4 DOI definition
  * @info: the Generic NETLINK info block
+ * @audit_info: NetLabel audit information
  *
  * Description:
- * Create a new CIPSO_V4_MAP_STD DOI definition based on the given ADD message
- * and add it to the CIPSO V4 engine.  Return zero on success and non-zero on
- * error.
+ * Create a new CIPSO_V4_MAP_TRANS DOI definition based on the given ADD
+ * message and add it to the CIPSO V4 engine.  Return zero on success and
+ * non-zero on error.
  *
  */
-static int netlbl_cipsov4_add_std(struct genl_info *info)
+static int netlbl_cipsov4_add_std(struct genl_info *info,
+                                 struct netlbl_audit *audit_info)
 {
        int ret_val = -EINVAL;
        struct cipso_v4_doi *doi_def = NULL;
@@ -181,7 +167,7 @@ static int netlbl_cipsov4_add_std(struct genl_info *info)
                ret_val = -ENOMEM;
                goto add_std_failure;
        }
-       doi_def->type = CIPSO_V4_MAP_STD;
+       doi_def->type = CIPSO_V4_MAP_TRANS;
 
        ret_val = netlbl_cipsov4_add_common(info, doi_def);
        if (ret_val != 0)
@@ -191,13 +177,13 @@ static int netlbl_cipsov4_add_std(struct genl_info *info)
        nla_for_each_nested(nla_a,
                            info->attrs[NLBL_CIPSOV4_A_MLSLVLLST],
                            nla_a_rem)
-               if (nla_a->nla_type == NLBL_CIPSOV4_A_MLSLVL) {
+               if (nla_type(nla_a) == NLBL_CIPSOV4_A_MLSLVL) {
                        if (nla_validate_nested(nla_a,
                                            NLBL_CIPSOV4_A_MAX,
                                            netlbl_cipsov4_genl_policy) != 0)
                                        goto add_std_failure;
                        nla_for_each_nested(nla_b, nla_a, nla_b_rem)
-                               switch (nla_b->nla_type) {
+                               switch (nla_type(nla_b)) {
                                case NLBL_CIPSOV4_A_MLSLVLLOC:
                                        if (nla_get_u32(nla_b) >
                                            CIPSO_V4_MAX_LOC_LVLS)
@@ -239,7 +225,7 @@ static int netlbl_cipsov4_add_std(struct genl_info *info)
        nla_for_each_nested(nla_a,
                            info->attrs[NLBL_CIPSOV4_A_MLSLVLLST],
                            nla_a_rem)
-               if (nla_a->nla_type == NLBL_CIPSOV4_A_MLSLVL) {
+               if (nla_type(nla_a) == NLBL_CIPSOV4_A_MLSLVL) {
                        struct nlattr *lvl_loc;
                        struct nlattr *lvl_rem;
 
@@ -264,13 +250,13 @@ static int netlbl_cipsov4_add_std(struct genl_info *info)
                nla_for_each_nested(nla_a,
                                    info->attrs[NLBL_CIPSOV4_A_MLSCATLST],
                                    nla_a_rem)
-                       if (nla_a->nla_type == NLBL_CIPSOV4_A_MLSCAT) {
+                       if (nla_type(nla_a) == NLBL_CIPSOV4_A_MLSCAT) {
                                if (nla_validate_nested(nla_a,
                                              NLBL_CIPSOV4_A_MAX,
                                              netlbl_cipsov4_genl_policy) != 0)
                                        goto add_std_failure;
                                nla_for_each_nested(nla_b, nla_a, nla_b_rem)
-                                       switch (nla_b->nla_type) {
+                                       switch (nla_type(nla_b)) {
                                        case NLBL_CIPSOV4_A_MLSCATLOC:
                                                if (nla_get_u32(nla_b) >
                                                    CIPSO_V4_MAX_LOC_CATS)
@@ -292,7 +278,7 @@ static int netlbl_cipsov4_add_std(struct genl_info *info)
                                        }
                        }
                doi_def->map.std->cat.local = kcalloc(
-                                             doi_def->map.std->cat.local_size,
+                                             doi_def->map.std->cat.local_size,
                                              sizeof(u32),
                                              GFP_KERNEL);
                if (doi_def->map.std->cat.local == NULL) {
@@ -300,7 +286,7 @@ static int netlbl_cipsov4_add_std(struct genl_info *info)
                        goto add_std_failure;
                }
                doi_def->map.std->cat.cipso = kcalloc(
-                                             doi_def->map.std->cat.cipso_size,
+                                             doi_def->map.std->cat.cipso_size,
                                              sizeof(u32),
                                              GFP_KERNEL);
                if (doi_def->map.std->cat.cipso == NULL) {
@@ -314,7 +300,7 @@ static int netlbl_cipsov4_add_std(struct genl_info *info)
                nla_for_each_nested(nla_a,
                                    info->attrs[NLBL_CIPSOV4_A_MLSCATLST],
                                    nla_a_rem)
-                       if (nla_a->nla_type == NLBL_CIPSOV4_A_MLSCAT) {
+                       if (nla_type(nla_a) == NLBL_CIPSOV4_A_MLSCAT) {
                                struct nlattr *cat_loc;
                                struct nlattr *cat_rem;
 
@@ -325,28 +311,29 @@ static int netlbl_cipsov4_add_std(struct genl_info *info)
                                if (cat_loc == NULL || cat_rem == NULL)
                                        goto add_std_failure;
                                doi_def->map.std->cat.local[
-                                                       nla_get_u32(cat_loc)] =
+                                                       nla_get_u32(cat_loc)] =
                                        nla_get_u32(cat_rem);
                                doi_def->map.std->cat.cipso[
-                                                       nla_get_u32(cat_rem)] =
+                                                       nla_get_u32(cat_rem)] =
                                        nla_get_u32(cat_loc);
                        }
        }
 
-       ret_val = cipso_v4_doi_add(doi_def);
+       ret_val = cipso_v4_doi_add(doi_def, audit_info);
        if (ret_val != 0)
                goto add_std_failure;
        return 0;
 
 add_std_failure:
        if (doi_def)
-               netlbl_cipsov4_doi_free(&doi_def->rcu);
+               cipso_v4_doi_free(doi_def);
        return ret_val;
 }
 
 /**
  * netlbl_cipsov4_add_pass - Adds a CIPSO V4 DOI definition
  * @info: the Generic NETLINK info block
+ * @audit_info: NetLabel audit information
  *
  * Description:
  * Create a new CIPSO_V4_MAP_PASS DOI definition based on the given ADD message
@@ -354,7 +341,8 @@ add_std_failure:
  * error.
  *
  */
-static int netlbl_cipsov4_add_pass(struct genl_info *info)
+static int netlbl_cipsov4_add_pass(struct genl_info *info,
+                                  struct netlbl_audit *audit_info)
 {
        int ret_val;
        struct cipso_v4_doi *doi_def = NULL;
@@ -371,13 +359,52 @@ static int netlbl_cipsov4_add_pass(struct genl_info *info)
        if (ret_val != 0)
                goto add_pass_failure;
 
-       ret_val = cipso_v4_doi_add(doi_def);
+       ret_val = cipso_v4_doi_add(doi_def, audit_info);
        if (ret_val != 0)
                goto add_pass_failure;
        return 0;
 
 add_pass_failure:
-       netlbl_cipsov4_doi_free(&doi_def->rcu);
+       cipso_v4_doi_free(doi_def);
+       return ret_val;
+}
+
+/**
+ * netlbl_cipsov4_add_local - Adds a CIPSO V4 DOI definition
+ * @info: the Generic NETLINK info block
+ * @audit_info: NetLabel audit information
+ *
+ * Description:
+ * Create a new CIPSO_V4_MAP_LOCAL DOI definition based on the given ADD
+ * message and add it to the CIPSO V4 engine.  Return zero on success and
+ * non-zero on error.
+ *
+ */
+static int netlbl_cipsov4_add_local(struct genl_info *info,
+                                   struct netlbl_audit *audit_info)
+{
+       int ret_val;
+       struct cipso_v4_doi *doi_def = NULL;
+
+       if (!info->attrs[NLBL_CIPSOV4_A_TAGLST])
+               return -EINVAL;
+
+       doi_def = kmalloc(sizeof(*doi_def), GFP_KERNEL);
+       if (doi_def == NULL)
+               return -ENOMEM;
+       doi_def->type = CIPSO_V4_MAP_LOCAL;
+
+       ret_val = netlbl_cipsov4_add_common(info, doi_def);
+       if (ret_val != 0)
+               goto add_local_failure;
+
+       ret_val = cipso_v4_doi_add(doi_def, audit_info);
+       if (ret_val != 0)
+               goto add_local_failure;
+       return 0;
+
+add_local_failure:
+       cipso_v4_doi_free(doi_def);
        return ret_val;
 }
 
@@ -395,41 +422,30 @@ static int netlbl_cipsov4_add(struct sk_buff *skb, struct genl_info *info)
 
 {
        int ret_val = -EINVAL;
-       u32 type;
-       u32 doi;
        const char *type_str = "(unknown)";
-       struct audit_buffer *audit_buf;
        struct netlbl_audit audit_info;
 
        if (!info->attrs[NLBL_CIPSOV4_A_DOI] ||
            !info->attrs[NLBL_CIPSOV4_A_MTYPE])
                return -EINVAL;
 
-       doi = nla_get_u32(info->attrs[NLBL_CIPSOV4_A_DOI]);
        netlbl_netlink_auditinfo(skb, &audit_info);
-
-       type = nla_get_u32(info->attrs[NLBL_CIPSOV4_A_MTYPE]);
-       switch (type) {
-       case CIPSO_V4_MAP_STD:
-               type_str = "std";
-               ret_val = netlbl_cipsov4_add_std(info);
+       switch (nla_get_u32(info->attrs[NLBL_CIPSOV4_A_MTYPE])) {
+       case CIPSO_V4_MAP_TRANS:
+               type_str = "trans";
+               ret_val = netlbl_cipsov4_add_std(info, &audit_info);
                break;
        case CIPSO_V4_MAP_PASS:
                type_str = "pass";
-               ret_val = netlbl_cipsov4_add_pass(info);
+               ret_val = netlbl_cipsov4_add_pass(info, &audit_info);
+               break;
+       case CIPSO_V4_MAP_LOCAL:
+               type_str = "local";
+               ret_val = netlbl_cipsov4_add_local(info, &audit_info);
                break;
        }
-
-       audit_buf = netlbl_audit_start_common(AUDIT_MAC_CIPSOV4_ADD,
-                                             &audit_info);
-       if (audit_buf != NULL) {
-               audit_log_format(audit_buf,
-                                " cipso_doi=%u cipso_type=%s res=%u",
-                                doi,
-                                type_str,
-                                ret_val == 0 ? 1 : 0);
-               audit_log_end(audit_buf);
-       }
+       if (ret_val == 0)
+               atomic_inc(&netlabel_mgmt_protocount);
 
        return ret_val;
 }
@@ -487,7 +503,7 @@ list_start:
        doi_def = cipso_v4_doi_getdef(doi);
        if (doi_def == NULL) {
                ret_val = -EINVAL;
-               goto list_failure;
+               goto list_failure_lock;
        }
 
        ret_val = nla_put_u32(ans_skb, NLBL_CIPSOV4_A_MTYPE, doi_def->type);
@@ -512,7 +528,7 @@ list_start:
        nla_nest_end(ans_skb, nla_a);
 
        switch (doi_def->type) {
-       case CIPSO_V4_MAP_STD:
+       case CIPSO_V4_MAP_TRANS:
                nla_a = nla_nest_start(ans_skb, NLBL_CIPSOV4_A_MLSLVLLST);
                if (nla_a == NULL) {
                        ret_val = -ENOMEM;
@@ -580,19 +596,14 @@ list_start:
        rcu_read_unlock();
 
        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_retry:
        /* XXX - this limit is a guesstimate */
        if (nlsze_mult < 4) {
                rcu_read_unlock();
                kfree_skb(ans_skb);
-               nlsze_mult++;
+               nlsze_mult *= 2;
                goto list_start;
        }
 list_failure_lock:
@@ -656,7 +667,7 @@ static int netlbl_cipsov4_listall(struct sk_buff *skb,
                                  struct netlink_callback *cb)
 {
        struct netlbl_cipsov4_doiwalk_arg cb_arg;
-       int doi_skip = cb->args[0];
+       u32 doi_skip = cb->args[0];
 
        cb_arg.nl_cb = cb;
        cb_arg.skb = skb;
@@ -669,6 +680,29 @@ static int netlbl_cipsov4_listall(struct sk_buff *skb,
 }
 
 /**
+ * netlbl_cipsov4_remove_cb - netlbl_cipsov4_remove() callback for REMOVE
+ * @entry: LSM domain mapping entry
+ * @arg: the netlbl_domhsh_walk_arg structure
+ *
+ * Description:
+ * This function is intended for use by netlbl_cipsov4_remove() as the callback
+ * for the netlbl_domhsh_walk() function; it removes LSM domain map entries
+ * which are associated with the CIPSO DOI specified in @arg.  Returns zero on
+ * success, negative values on failure.
+ *
+ */
+static int netlbl_cipsov4_remove_cb(struct netlbl_dom_map *entry, void *arg)
+{
+       struct netlbl_domhsh_walk_arg *cb_arg = arg;
+
+       if (entry->type == NETLBL_NLTYPE_CIPSOV4 &&
+           entry->type_def.cipsov4->doi == cb_arg->doi)
+               return netlbl_domhsh_remove_entry(entry, cb_arg->audit_info);
+
+       return 0;
+}
+
+/**
  * netlbl_cipsov4_remove - Handle a REMOVE message
  * @skb: the NETLINK buffer
  * @info: the Generic NETLINK info block
@@ -681,28 +715,23 @@ static int netlbl_cipsov4_listall(struct sk_buff *skb,
 static int netlbl_cipsov4_remove(struct sk_buff *skb, struct genl_info *info)
 {
        int ret_val = -EINVAL;
-       u32 doi = 0;
-       struct audit_buffer *audit_buf;
+       struct netlbl_domhsh_walk_arg cb_arg;
        struct netlbl_audit audit_info;
+       u32 skip_bkt = 0;
+       u32 skip_chain = 0;
 
        if (!info->attrs[NLBL_CIPSOV4_A_DOI])
                return -EINVAL;
 
-       doi = nla_get_u32(info->attrs[NLBL_CIPSOV4_A_DOI]);
        netlbl_netlink_auditinfo(skb, &audit_info);
-
-       ret_val = cipso_v4_doi_remove(doi,
-                                     &audit_info,
-                                     netlbl_cipsov4_doi_free);
-
-       audit_buf = netlbl_audit_start_common(AUDIT_MAC_CIPSOV4_DEL,
-                                             &audit_info);
-       if (audit_buf != NULL) {
-               audit_log_format(audit_buf,
-                                " cipso_doi=%u res=%u",
-                                doi,
-                                ret_val == 0 ? 1 : 0);
-               audit_log_end(audit_buf);
+       cb_arg.doi = nla_get_u32(info->attrs[NLBL_CIPSOV4_A_DOI]);
+       cb_arg.audit_info = &audit_info;
+       ret_val = netlbl_domhsh_walk(&skip_bkt, &skip_chain,
+                                    netlbl_cipsov4_remove_cb, &cb_arg);
+       if (ret_val == 0 || ret_val == -ENOENT) {
+               ret_val = cipso_v4_doi_remove(cb_arg.doi, &audit_info);
+               if (ret_val == 0)
+                       atomic_dec(&netlabel_mgmt_protocount);
        }
 
        return ret_val;
@@ -712,36 +741,35 @@ static int netlbl_cipsov4_remove(struct sk_buff *skb, struct genl_info *info)
  * NetLabel Generic NETLINK Command Definitions
  */
 
-static struct genl_ops netlbl_cipsov4_genl_c_add = {
+static struct genl_ops netlbl_cipsov4_ops[] = {
+       {
        .cmd = NLBL_CIPSOV4_C_ADD,
        .flags = GENL_ADMIN_PERM,
        .policy = netlbl_cipsov4_genl_policy,
        .doit = netlbl_cipsov4_add,
        .dumpit = NULL,
-};
-
-static struct genl_ops netlbl_cipsov4_genl_c_remove = {
+       },
+       {
        .cmd = NLBL_CIPSOV4_C_REMOVE,
        .flags = GENL_ADMIN_PERM,
        .policy = netlbl_cipsov4_genl_policy,
        .doit = netlbl_cipsov4_remove,
        .dumpit = NULL,
-};
-
-static struct genl_ops netlbl_cipsov4_genl_c_list = {
+       },
+       {
        .cmd = NLBL_CIPSOV4_C_LIST,
        .flags = 0,
        .policy = netlbl_cipsov4_genl_policy,
        .doit = netlbl_cipsov4_list,
        .dumpit = NULL,
-};
-
-static struct genl_ops netlbl_cipsov4_genl_c_listall = {
+       },
+       {
        .cmd = NLBL_CIPSOV4_C_LISTALL,
        .flags = 0,
        .policy = netlbl_cipsov4_genl_policy,
        .doit = NULL,
        .dumpit = netlbl_cipsov4_listall,
+       },
 };
 
 /*
@@ -756,30 +784,8 @@ static struct genl_ops netlbl_cipsov4_genl_c_listall = {
  * mechanism.  Returns zero on success, negative values on failure.
  *
  */
-int netlbl_cipsov4_genl_init(void)
+int __init netlbl_cipsov4_genl_init(void)
 {
-       int ret_val;
-
-       ret_val = genl_register_family(&netlbl_cipsov4_gnl_family);
-       if (ret_val != 0)
-               return ret_val;
-
-       ret_val = genl_register_ops(&netlbl_cipsov4_gnl_family,
-                                   &netlbl_cipsov4_genl_c_add);
-       if (ret_val != 0)
-               return ret_val;
-       ret_val = genl_register_ops(&netlbl_cipsov4_gnl_family,
-                                   &netlbl_cipsov4_genl_c_remove);
-       if (ret_val != 0)
-               return ret_val;
-       ret_val = genl_register_ops(&netlbl_cipsov4_gnl_family,
-                                   &netlbl_cipsov4_genl_c_list);
-       if (ret_val != 0)
-               return ret_val;
-       ret_val = genl_register_ops(&netlbl_cipsov4_gnl_family,
-                                   &netlbl_cipsov4_genl_c_listall);
-       if (ret_val != 0)
-               return ret_val;
-
-       return 0;
+       return genl_register_family_with_ops(&netlbl_cipsov4_gnl_family,
+               netlbl_cipsov4_ops, ARRAY_SIZE(netlbl_cipsov4_ops));
 }