2 * CALIPSO - Common Architecture Label IPv6 Security Option
4 * This is an implementation of the CALIPSO protocol as specified in
7 * Authors: Paul Moore <paul.moore@hp.com>
8 * Huw Davies <huw@codeweavers.com>
12 /* (c) Copyright Hewlett-Packard Development Company, L.P., 2006, 2008
13 * (c) Copyright Huw Davies <huw@codeweavers.com>, 2015
15 * This program is free software; you can redistribute it and/or modify
16 * it under the terms of the GNU General Public License as published by
17 * the Free Software Foundation; either version 2 of the License, or
18 * (at your option) any later version.
20 * This program is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
23 * the GNU General Public License for more details.
25 * You should have received a copy of the GNU General Public License
26 * along with this program; if not, see <http://www.gnu.org/licenses/>.
30 #include <linux/init.h>
31 #include <linux/types.h>
32 #include <linux/rcupdate.h>
33 #include <linux/list.h>
34 #include <linux/spinlock.h>
35 #include <linux/string.h>
36 #include <linux/jhash.h>
37 #include <linux/audit.h>
38 #include <linux/slab.h>
42 #include <net/netlabel.h>
43 #include <net/calipso.h>
44 #include <linux/atomic.h>
45 #include <linux/bug.h>
46 #include <asm/unaligned.h>
48 /* List of available DOI definitions */
49 static DEFINE_SPINLOCK(calipso_doi_list_lock);
50 static LIST_HEAD(calipso_doi_list);
56 * calipso_doi_search - Searches for a DOI definition
57 * @doi: the DOI to search for
60 * Search the DOI definition list for a DOI definition with a DOI value that
61 * matches @doi. The caller is responsible for calling rcu_read_[un]lock().
62 * Returns a pointer to the DOI definition on success and NULL on failure.
64 static struct calipso_doi *calipso_doi_search(u32 doi)
66 struct calipso_doi *iter;
68 list_for_each_entry_rcu(iter, &calipso_doi_list, list)
69 if (iter->doi == doi && atomic_read(&iter->refcount))
75 * calipso_doi_add - Add a new DOI to the CALIPSO protocol engine
76 * @doi_def: the DOI structure
77 * @audit_info: NetLabel audit information
80 * The caller defines a new DOI for use by the CALIPSO engine and calls this
81 * function to add it to the list of acceptable domains. The caller must
82 * ensure that the mapping table specified in @doi_def->map meets all of the
83 * requirements of the mapping type (see calipso.h for details). Returns
84 * zero on success and non-zero on failure.
87 static int calipso_doi_add(struct calipso_doi *doi_def,
88 struct netlbl_audit *audit_info)
90 int ret_val = -EINVAL;
93 struct audit_buffer *audit_buf;
96 doi_type = doi_def->type;
98 if (doi_def->doi == CALIPSO_DOI_UNKNOWN)
101 atomic_set(&doi_def->refcount, 1);
103 spin_lock(&calipso_doi_list_lock);
104 if (calipso_doi_search(doi_def->doi)) {
105 spin_unlock(&calipso_doi_list_lock);
109 list_add_tail_rcu(&doi_def->list, &calipso_doi_list);
110 spin_unlock(&calipso_doi_list_lock);
114 audit_buf = netlbl_audit_start(AUDIT_MAC_CALIPSO_ADD, audit_info);
116 const char *type_str;
119 case CALIPSO_MAP_PASS:
123 type_str = "(unknown)";
125 audit_log_format(audit_buf,
126 " calipso_doi=%u calipso_type=%s res=%u",
127 doi, type_str, ret_val == 0 ? 1 : 0);
128 audit_log_end(audit_buf);
135 * calipso_doi_free - Frees a DOI definition
136 * @doi_def: the DOI definition
139 * This function frees all of the memory associated with a DOI definition.
142 static void calipso_doi_free(struct calipso_doi *doi_def)
147 static const struct netlbl_calipso_ops ops = {
148 .doi_add = calipso_doi_add,
149 .doi_free = calipso_doi_free,
153 * calipso_init - Initialize the CALIPSO module
156 * Initialize the CALIPSO module and prepare it for use. Returns zero on
157 * success and negative values on failure.
160 int __init calipso_init(void)
162 netlbl_calipso_ops_register(&ops);
166 void calipso_exit(void)
168 netlbl_calipso_ops_register(NULL);