Commit 4c3a0a254e5d706d3fe01bf42261534858d05586
Committed by
David S. Miller
1 parent
0f8f27c395
[NETLABEL]: Fix lookup logic of netlbl_domhsh_search_def.
Currently, if the call to netlbl_domhsh_search succeeds the return result will still be NULL. Fix that, by returning the found entry (if any). Signed-off-by: Pavel Emelyanov <xemul@openvz.org> Acked-by: Paul Moore <paul.moore@hp.com> Signed-off-by: David S. Miller <davem@davemloft.net>
Showing 1 changed file with 3 additions and 3 deletions Inline Diff
net/netlabel/netlabel_domainhash.c
1 | /* | 1 | /* |
2 | * NetLabel Domain Hash Table | 2 | * NetLabel Domain Hash Table |
3 | * | 3 | * |
4 | * This file manages the domain hash table that NetLabel uses to determine | 4 | * This file manages the domain hash table that NetLabel uses to determine |
5 | * which network labeling protocol to use for a given domain. The NetLabel | 5 | * which network labeling protocol to use for a given domain. The NetLabel |
6 | * system manages static and dynamic label mappings for network protocols such | 6 | * system manages static and dynamic label mappings for network protocols such |
7 | * as CIPSO and RIPSO. | 7 | * as CIPSO and RIPSO. |
8 | * | 8 | * |
9 | * Author: Paul Moore <paul.moore@hp.com> | 9 | * Author: Paul Moore <paul.moore@hp.com> |
10 | * | 10 | * |
11 | */ | 11 | */ |
12 | 12 | ||
13 | /* | 13 | /* |
14 | * (c) Copyright Hewlett-Packard Development Company, L.P., 2006 | 14 | * (c) Copyright Hewlett-Packard Development Company, L.P., 2006 |
15 | * | 15 | * |
16 | * This program is free software; you can redistribute it and/or modify | 16 | * This program is free software; you can redistribute it and/or modify |
17 | * it under the terms of the GNU General Public License as published by | 17 | * it under the terms of the GNU General Public License as published by |
18 | * the Free Software Foundation; either version 2 of the License, or | 18 | * the Free Software Foundation; either version 2 of the License, or |
19 | * (at your option) any later version. | 19 | * (at your option) any later version. |
20 | * | 20 | * |
21 | * This program is distributed in the hope that it will be useful, | 21 | * This program is distributed in the hope that it will be useful, |
22 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | 22 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
23 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See | 23 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See |
24 | * the GNU General Public License for more details. | 24 | * the GNU General Public License for more details. |
25 | * | 25 | * |
26 | * You should have received a copy of the GNU General Public License | 26 | * You should have received a copy of the GNU General Public License |
27 | * along with this program; if not, write to the Free Software | 27 | * along with this program; if not, write to the Free Software |
28 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | 28 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
29 | * | 29 | * |
30 | */ | 30 | */ |
31 | 31 | ||
32 | #include <linux/types.h> | 32 | #include <linux/types.h> |
33 | #include <linux/rcupdate.h> | 33 | #include <linux/rcupdate.h> |
34 | #include <linux/list.h> | 34 | #include <linux/list.h> |
35 | #include <linux/skbuff.h> | 35 | #include <linux/skbuff.h> |
36 | #include <linux/spinlock.h> | 36 | #include <linux/spinlock.h> |
37 | #include <linux/string.h> | 37 | #include <linux/string.h> |
38 | #include <linux/audit.h> | 38 | #include <linux/audit.h> |
39 | #include <net/netlabel.h> | 39 | #include <net/netlabel.h> |
40 | #include <net/cipso_ipv4.h> | 40 | #include <net/cipso_ipv4.h> |
41 | #include <asm/bug.h> | 41 | #include <asm/bug.h> |
42 | 42 | ||
43 | #include "netlabel_mgmt.h" | 43 | #include "netlabel_mgmt.h" |
44 | #include "netlabel_domainhash.h" | 44 | #include "netlabel_domainhash.h" |
45 | #include "netlabel_user.h" | 45 | #include "netlabel_user.h" |
46 | 46 | ||
47 | struct netlbl_domhsh_tbl { | 47 | struct netlbl_domhsh_tbl { |
48 | struct list_head *tbl; | 48 | struct list_head *tbl; |
49 | u32 size; | 49 | u32 size; |
50 | }; | 50 | }; |
51 | 51 | ||
52 | /* Domain hash table */ | 52 | /* Domain hash table */ |
53 | /* XXX - updates should be so rare that having one spinlock for the entire | 53 | /* XXX - updates should be so rare that having one spinlock for the entire |
54 | * hash table should be okay */ | 54 | * hash table should be okay */ |
55 | static DEFINE_SPINLOCK(netlbl_domhsh_lock); | 55 | static DEFINE_SPINLOCK(netlbl_domhsh_lock); |
56 | static struct netlbl_domhsh_tbl *netlbl_domhsh = NULL; | 56 | static struct netlbl_domhsh_tbl *netlbl_domhsh = NULL; |
57 | static struct netlbl_dom_map *netlbl_domhsh_def = NULL; | 57 | static struct netlbl_dom_map *netlbl_domhsh_def = NULL; |
58 | 58 | ||
59 | /* | 59 | /* |
60 | * Domain Hash Table Helper Functions | 60 | * Domain Hash Table Helper Functions |
61 | */ | 61 | */ |
62 | 62 | ||
63 | /** | 63 | /** |
64 | * netlbl_domhsh_free_entry - Frees a domain hash table entry | 64 | * netlbl_domhsh_free_entry - Frees a domain hash table entry |
65 | * @entry: the entry's RCU field | 65 | * @entry: the entry's RCU field |
66 | * | 66 | * |
67 | * Description: | 67 | * Description: |
68 | * This function is designed to be used as a callback to the call_rcu() | 68 | * This function is designed to be used as a callback to the call_rcu() |
69 | * function so that the memory allocated to a hash table entry can be released | 69 | * function so that the memory allocated to a hash table entry can be released |
70 | * safely. | 70 | * safely. |
71 | * | 71 | * |
72 | */ | 72 | */ |
73 | static void netlbl_domhsh_free_entry(struct rcu_head *entry) | 73 | static void netlbl_domhsh_free_entry(struct rcu_head *entry) |
74 | { | 74 | { |
75 | struct netlbl_dom_map *ptr; | 75 | struct netlbl_dom_map *ptr; |
76 | 76 | ||
77 | ptr = container_of(entry, struct netlbl_dom_map, rcu); | 77 | ptr = container_of(entry, struct netlbl_dom_map, rcu); |
78 | kfree(ptr->domain); | 78 | kfree(ptr->domain); |
79 | kfree(ptr); | 79 | kfree(ptr); |
80 | } | 80 | } |
81 | 81 | ||
82 | /** | 82 | /** |
83 | * netlbl_domhsh_hash - Hashing function for the domain hash table | 83 | * netlbl_domhsh_hash - Hashing function for the domain hash table |
84 | * @domain: the domain name to hash | 84 | * @domain: the domain name to hash |
85 | * | 85 | * |
86 | * Description: | 86 | * Description: |
87 | * This is the hashing function for the domain hash table, it returns the | 87 | * This is the hashing function for the domain hash table, it returns the |
88 | * correct bucket number for the domain. The caller is responsibile for | 88 | * correct bucket number for the domain. The caller is responsibile for |
89 | * calling the rcu_read_[un]lock() functions. | 89 | * calling the rcu_read_[un]lock() functions. |
90 | * | 90 | * |
91 | */ | 91 | */ |
92 | static u32 netlbl_domhsh_hash(const char *key) | 92 | static u32 netlbl_domhsh_hash(const char *key) |
93 | { | 93 | { |
94 | u32 iter; | 94 | u32 iter; |
95 | u32 val; | 95 | u32 val; |
96 | u32 len; | 96 | u32 len; |
97 | 97 | ||
98 | /* This is taken (with slight modification) from | 98 | /* This is taken (with slight modification) from |
99 | * security/selinux/ss/symtab.c:symhash() */ | 99 | * security/selinux/ss/symtab.c:symhash() */ |
100 | 100 | ||
101 | for (iter = 0, val = 0, len = strlen(key); iter < len; iter++) | 101 | for (iter = 0, val = 0, len = strlen(key); iter < len; iter++) |
102 | val = (val << 4 | (val >> (8 * sizeof(u32) - 4))) ^ key[iter]; | 102 | val = (val << 4 | (val >> (8 * sizeof(u32) - 4))) ^ key[iter]; |
103 | return val & (rcu_dereference(netlbl_domhsh)->size - 1); | 103 | return val & (rcu_dereference(netlbl_domhsh)->size - 1); |
104 | } | 104 | } |
105 | 105 | ||
106 | /** | 106 | /** |
107 | * netlbl_domhsh_search - Search for a domain entry | 107 | * netlbl_domhsh_search - Search for a domain entry |
108 | * @domain: the domain | 108 | * @domain: the domain |
109 | * | 109 | * |
110 | * Description: | 110 | * Description: |
111 | * Searches the domain hash table and returns a pointer to the hash table | 111 | * Searches the domain hash table and returns a pointer to the hash table |
112 | * entry if found, otherwise NULL is returned. The caller is responsibile for | 112 | * entry if found, otherwise NULL is returned. The caller is responsibile for |
113 | * the rcu hash table locks (i.e. the caller much call rcu_read_[un]lock()). | 113 | * the rcu hash table locks (i.e. the caller much call rcu_read_[un]lock()). |
114 | * | 114 | * |
115 | */ | 115 | */ |
116 | static struct netlbl_dom_map *netlbl_domhsh_search(const char *domain) | 116 | static struct netlbl_dom_map *netlbl_domhsh_search(const char *domain) |
117 | { | 117 | { |
118 | u32 bkt; | 118 | u32 bkt; |
119 | struct netlbl_dom_map *iter; | 119 | struct netlbl_dom_map *iter; |
120 | 120 | ||
121 | if (domain != NULL) { | 121 | if (domain != NULL) { |
122 | bkt = netlbl_domhsh_hash(domain); | 122 | bkt = netlbl_domhsh_hash(domain); |
123 | list_for_each_entry_rcu(iter, | 123 | list_for_each_entry_rcu(iter, |
124 | &rcu_dereference(netlbl_domhsh)->tbl[bkt], | 124 | &rcu_dereference(netlbl_domhsh)->tbl[bkt], |
125 | list) | 125 | list) |
126 | if (iter->valid && strcmp(iter->domain, domain) == 0) | 126 | if (iter->valid && strcmp(iter->domain, domain) == 0) |
127 | return iter; | 127 | return iter; |
128 | } | 128 | } |
129 | 129 | ||
130 | return NULL; | 130 | return NULL; |
131 | } | 131 | } |
132 | 132 | ||
133 | /** | 133 | /** |
134 | * netlbl_domhsh_search_def - Search for a domain entry | 134 | * netlbl_domhsh_search_def - Search for a domain entry |
135 | * @domain: the domain | 135 | * @domain: the domain |
136 | * @def: return default if no match is found | 136 | * @def: return default if no match is found |
137 | * | 137 | * |
138 | * Description: | 138 | * Description: |
139 | * Searches the domain hash table and returns a pointer to the hash table | 139 | * Searches the domain hash table and returns a pointer to the hash table |
140 | * entry if an exact match is found, if an exact match is not present in the | 140 | * entry if an exact match is found, if an exact match is not present in the |
141 | * hash table then the default entry is returned if valid otherwise NULL is | 141 | * hash table then the default entry is returned if valid otherwise NULL is |
142 | * returned. The caller is responsibile for the rcu hash table locks | 142 | * returned. The caller is responsibile for the rcu hash table locks |
143 | * (i.e. the caller much call rcu_read_[un]lock()). | 143 | * (i.e. the caller much call rcu_read_[un]lock()). |
144 | * | 144 | * |
145 | */ | 145 | */ |
146 | static struct netlbl_dom_map *netlbl_domhsh_search_def(const char *domain) | 146 | static struct netlbl_dom_map *netlbl_domhsh_search_def(const char *domain) |
147 | { | 147 | { |
148 | struct netlbl_dom_map *entry; | 148 | struct netlbl_dom_map *entry; |
149 | 149 | ||
150 | entry = netlbl_domhsh_search(domain); | 150 | entry = netlbl_domhsh_search(domain); |
151 | if (entry == NULL) { | 151 | if (entry == NULL) { |
152 | entry = rcu_dereference(netlbl_domhsh_def); | 152 | entry = rcu_dereference(netlbl_domhsh_def); |
153 | if (entry != NULL && entry->valid) | 153 | if (entry != NULL && !entry->valid) |
154 | return entry; | 154 | entry = NULL; |
155 | } | 155 | } |
156 | 156 | ||
157 | return NULL; | 157 | return entry; |
158 | } | 158 | } |
159 | 159 | ||
160 | /* | 160 | /* |
161 | * Domain Hash Table Functions | 161 | * Domain Hash Table Functions |
162 | */ | 162 | */ |
163 | 163 | ||
164 | /** | 164 | /** |
165 | * netlbl_domhsh_init - Init for the domain hash | 165 | * netlbl_domhsh_init - Init for the domain hash |
166 | * @size: the number of bits to use for the hash buckets | 166 | * @size: the number of bits to use for the hash buckets |
167 | * | 167 | * |
168 | * Description: | 168 | * Description: |
169 | * Initializes the domain hash table, should be called only by | 169 | * Initializes the domain hash table, should be called only by |
170 | * netlbl_user_init() during initialization. Returns zero on success, non-zero | 170 | * netlbl_user_init() during initialization. Returns zero on success, non-zero |
171 | * values on error. | 171 | * values on error. |
172 | * | 172 | * |
173 | */ | 173 | */ |
174 | int netlbl_domhsh_init(u32 size) | 174 | int netlbl_domhsh_init(u32 size) |
175 | { | 175 | { |
176 | u32 iter; | 176 | u32 iter; |
177 | struct netlbl_domhsh_tbl *hsh_tbl; | 177 | struct netlbl_domhsh_tbl *hsh_tbl; |
178 | 178 | ||
179 | if (size == 0) | 179 | if (size == 0) |
180 | return -EINVAL; | 180 | return -EINVAL; |
181 | 181 | ||
182 | hsh_tbl = kmalloc(sizeof(*hsh_tbl), GFP_KERNEL); | 182 | hsh_tbl = kmalloc(sizeof(*hsh_tbl), GFP_KERNEL); |
183 | if (hsh_tbl == NULL) | 183 | if (hsh_tbl == NULL) |
184 | return -ENOMEM; | 184 | return -ENOMEM; |
185 | hsh_tbl->size = 1 << size; | 185 | hsh_tbl->size = 1 << size; |
186 | hsh_tbl->tbl = kcalloc(hsh_tbl->size, | 186 | hsh_tbl->tbl = kcalloc(hsh_tbl->size, |
187 | sizeof(struct list_head), | 187 | sizeof(struct list_head), |
188 | GFP_KERNEL); | 188 | GFP_KERNEL); |
189 | if (hsh_tbl->tbl == NULL) { | 189 | if (hsh_tbl->tbl == NULL) { |
190 | kfree(hsh_tbl); | 190 | kfree(hsh_tbl); |
191 | return -ENOMEM; | 191 | return -ENOMEM; |
192 | } | 192 | } |
193 | for (iter = 0; iter < hsh_tbl->size; iter++) | 193 | for (iter = 0; iter < hsh_tbl->size; iter++) |
194 | INIT_LIST_HEAD(&hsh_tbl->tbl[iter]); | 194 | INIT_LIST_HEAD(&hsh_tbl->tbl[iter]); |
195 | 195 | ||
196 | spin_lock(&netlbl_domhsh_lock); | 196 | spin_lock(&netlbl_domhsh_lock); |
197 | rcu_assign_pointer(netlbl_domhsh, hsh_tbl); | 197 | rcu_assign_pointer(netlbl_domhsh, hsh_tbl); |
198 | spin_unlock(&netlbl_domhsh_lock); | 198 | spin_unlock(&netlbl_domhsh_lock); |
199 | 199 | ||
200 | return 0; | 200 | return 0; |
201 | } | 201 | } |
202 | 202 | ||
203 | /** | 203 | /** |
204 | * netlbl_domhsh_add - Adds a entry to the domain hash table | 204 | * netlbl_domhsh_add - Adds a entry to the domain hash table |
205 | * @entry: the entry to add | 205 | * @entry: the entry to add |
206 | * @audit_info: NetLabel audit information | 206 | * @audit_info: NetLabel audit information |
207 | * | 207 | * |
208 | * Description: | 208 | * Description: |
209 | * Adds a new entry to the domain hash table and handles any updates to the | 209 | * Adds a new entry to the domain hash table and handles any updates to the |
210 | * lower level protocol handler (i.e. CIPSO). Returns zero on success, | 210 | * lower level protocol handler (i.e. CIPSO). Returns zero on success, |
211 | * negative on failure. | 211 | * negative on failure. |
212 | * | 212 | * |
213 | */ | 213 | */ |
214 | int netlbl_domhsh_add(struct netlbl_dom_map *entry, | 214 | int netlbl_domhsh_add(struct netlbl_dom_map *entry, |
215 | struct netlbl_audit *audit_info) | 215 | struct netlbl_audit *audit_info) |
216 | { | 216 | { |
217 | int ret_val; | 217 | int ret_val; |
218 | u32 bkt; | 218 | u32 bkt; |
219 | struct audit_buffer *audit_buf; | 219 | struct audit_buffer *audit_buf; |
220 | 220 | ||
221 | switch (entry->type) { | 221 | switch (entry->type) { |
222 | case NETLBL_NLTYPE_UNLABELED: | 222 | case NETLBL_NLTYPE_UNLABELED: |
223 | ret_val = 0; | 223 | ret_val = 0; |
224 | break; | 224 | break; |
225 | case NETLBL_NLTYPE_CIPSOV4: | 225 | case NETLBL_NLTYPE_CIPSOV4: |
226 | ret_val = cipso_v4_doi_domhsh_add(entry->type_def.cipsov4, | 226 | ret_val = cipso_v4_doi_domhsh_add(entry->type_def.cipsov4, |
227 | entry->domain); | 227 | entry->domain); |
228 | break; | 228 | break; |
229 | default: | 229 | default: |
230 | return -EINVAL; | 230 | return -EINVAL; |
231 | } | 231 | } |
232 | if (ret_val != 0) | 232 | if (ret_val != 0) |
233 | return ret_val; | 233 | return ret_val; |
234 | 234 | ||
235 | entry->valid = 1; | 235 | entry->valid = 1; |
236 | INIT_RCU_HEAD(&entry->rcu); | 236 | INIT_RCU_HEAD(&entry->rcu); |
237 | 237 | ||
238 | rcu_read_lock(); | 238 | rcu_read_lock(); |
239 | spin_lock(&netlbl_domhsh_lock); | 239 | spin_lock(&netlbl_domhsh_lock); |
240 | if (entry->domain != NULL) { | 240 | if (entry->domain != NULL) { |
241 | bkt = netlbl_domhsh_hash(entry->domain); | 241 | bkt = netlbl_domhsh_hash(entry->domain); |
242 | if (netlbl_domhsh_search(entry->domain) == NULL) | 242 | if (netlbl_domhsh_search(entry->domain) == NULL) |
243 | list_add_tail_rcu(&entry->list, | 243 | list_add_tail_rcu(&entry->list, |
244 | &rcu_dereference(netlbl_domhsh)->tbl[bkt]); | 244 | &rcu_dereference(netlbl_domhsh)->tbl[bkt]); |
245 | else | 245 | else |
246 | ret_val = -EEXIST; | 246 | ret_val = -EEXIST; |
247 | } else { | 247 | } else { |
248 | INIT_LIST_HEAD(&entry->list); | 248 | INIT_LIST_HEAD(&entry->list); |
249 | if (rcu_dereference(netlbl_domhsh_def) == NULL) | 249 | if (rcu_dereference(netlbl_domhsh_def) == NULL) |
250 | rcu_assign_pointer(netlbl_domhsh_def, entry); | 250 | rcu_assign_pointer(netlbl_domhsh_def, entry); |
251 | else | 251 | else |
252 | ret_val = -EEXIST; | 252 | ret_val = -EEXIST; |
253 | } | 253 | } |
254 | spin_unlock(&netlbl_domhsh_lock); | 254 | spin_unlock(&netlbl_domhsh_lock); |
255 | audit_buf = netlbl_audit_start_common(AUDIT_MAC_MAP_ADD, audit_info); | 255 | audit_buf = netlbl_audit_start_common(AUDIT_MAC_MAP_ADD, audit_info); |
256 | if (audit_buf != NULL) { | 256 | if (audit_buf != NULL) { |
257 | audit_log_format(audit_buf, | 257 | audit_log_format(audit_buf, |
258 | " nlbl_domain=%s", | 258 | " nlbl_domain=%s", |
259 | entry->domain ? entry->domain : "(default)"); | 259 | entry->domain ? entry->domain : "(default)"); |
260 | switch (entry->type) { | 260 | switch (entry->type) { |
261 | case NETLBL_NLTYPE_UNLABELED: | 261 | case NETLBL_NLTYPE_UNLABELED: |
262 | audit_log_format(audit_buf, " nlbl_protocol=unlbl"); | 262 | audit_log_format(audit_buf, " nlbl_protocol=unlbl"); |
263 | break; | 263 | break; |
264 | case NETLBL_NLTYPE_CIPSOV4: | 264 | case NETLBL_NLTYPE_CIPSOV4: |
265 | audit_log_format(audit_buf, | 265 | audit_log_format(audit_buf, |
266 | " nlbl_protocol=cipsov4 cipso_doi=%u", | 266 | " nlbl_protocol=cipsov4 cipso_doi=%u", |
267 | entry->type_def.cipsov4->doi); | 267 | entry->type_def.cipsov4->doi); |
268 | break; | 268 | break; |
269 | } | 269 | } |
270 | audit_log_format(audit_buf, " res=%u", ret_val == 0 ? 1 : 0); | 270 | audit_log_format(audit_buf, " res=%u", ret_val == 0 ? 1 : 0); |
271 | audit_log_end(audit_buf); | 271 | audit_log_end(audit_buf); |
272 | } | 272 | } |
273 | rcu_read_unlock(); | 273 | rcu_read_unlock(); |
274 | 274 | ||
275 | if (ret_val != 0) { | 275 | if (ret_val != 0) { |
276 | switch (entry->type) { | 276 | switch (entry->type) { |
277 | case NETLBL_NLTYPE_CIPSOV4: | 277 | case NETLBL_NLTYPE_CIPSOV4: |
278 | if (cipso_v4_doi_domhsh_remove(entry->type_def.cipsov4, | 278 | if (cipso_v4_doi_domhsh_remove(entry->type_def.cipsov4, |
279 | entry->domain) != 0) | 279 | entry->domain) != 0) |
280 | BUG(); | 280 | BUG(); |
281 | break; | 281 | break; |
282 | } | 282 | } |
283 | } | 283 | } |
284 | 284 | ||
285 | return ret_val; | 285 | return ret_val; |
286 | } | 286 | } |
287 | 287 | ||
288 | /** | 288 | /** |
289 | * netlbl_domhsh_add_default - Adds the default entry to the domain hash table | 289 | * netlbl_domhsh_add_default - Adds the default entry to the domain hash table |
290 | * @entry: the entry to add | 290 | * @entry: the entry to add |
291 | * @audit_info: NetLabel audit information | 291 | * @audit_info: NetLabel audit information |
292 | * | 292 | * |
293 | * Description: | 293 | * Description: |
294 | * Adds a new default entry to the domain hash table and handles any updates | 294 | * Adds a new default entry to the domain hash table and handles any updates |
295 | * to the lower level protocol handler (i.e. CIPSO). Returns zero on success, | 295 | * to the lower level protocol handler (i.e. CIPSO). Returns zero on success, |
296 | * negative on failure. | 296 | * negative on failure. |
297 | * | 297 | * |
298 | */ | 298 | */ |
299 | int netlbl_domhsh_add_default(struct netlbl_dom_map *entry, | 299 | int netlbl_domhsh_add_default(struct netlbl_dom_map *entry, |
300 | struct netlbl_audit *audit_info) | 300 | struct netlbl_audit *audit_info) |
301 | { | 301 | { |
302 | return netlbl_domhsh_add(entry, audit_info); | 302 | return netlbl_domhsh_add(entry, audit_info); |
303 | } | 303 | } |
304 | 304 | ||
305 | /** | 305 | /** |
306 | * netlbl_domhsh_remove - Removes an entry from the domain hash table | 306 | * netlbl_domhsh_remove - Removes an entry from the domain hash table |
307 | * @domain: the domain to remove | 307 | * @domain: the domain to remove |
308 | * @audit_info: NetLabel audit information | 308 | * @audit_info: NetLabel audit information |
309 | * | 309 | * |
310 | * Description: | 310 | * Description: |
311 | * Removes an entry from the domain hash table and handles any updates to the | 311 | * Removes an entry from the domain hash table and handles any updates to the |
312 | * lower level protocol handler (i.e. CIPSO). Returns zero on success, | 312 | * lower level protocol handler (i.e. CIPSO). Returns zero on success, |
313 | * negative on failure. | 313 | * negative on failure. |
314 | * | 314 | * |
315 | */ | 315 | */ |
316 | int netlbl_domhsh_remove(const char *domain, struct netlbl_audit *audit_info) | 316 | int netlbl_domhsh_remove(const char *domain, struct netlbl_audit *audit_info) |
317 | { | 317 | { |
318 | int ret_val = -ENOENT; | 318 | int ret_val = -ENOENT; |
319 | struct netlbl_dom_map *entry; | 319 | struct netlbl_dom_map *entry; |
320 | struct audit_buffer *audit_buf; | 320 | struct audit_buffer *audit_buf; |
321 | 321 | ||
322 | rcu_read_lock(); | 322 | rcu_read_lock(); |
323 | if (domain) | 323 | if (domain) |
324 | entry = netlbl_domhsh_search(domain); | 324 | entry = netlbl_domhsh_search(domain); |
325 | else | 325 | else |
326 | entry = netlbl_domhsh_search_def(domain); | 326 | entry = netlbl_domhsh_search_def(domain); |
327 | if (entry == NULL) | 327 | if (entry == NULL) |
328 | goto remove_return; | 328 | goto remove_return; |
329 | switch (entry->type) { | 329 | switch (entry->type) { |
330 | case NETLBL_NLTYPE_CIPSOV4: | 330 | case NETLBL_NLTYPE_CIPSOV4: |
331 | cipso_v4_doi_domhsh_remove(entry->type_def.cipsov4, | 331 | cipso_v4_doi_domhsh_remove(entry->type_def.cipsov4, |
332 | entry->domain); | 332 | entry->domain); |
333 | break; | 333 | break; |
334 | } | 334 | } |
335 | spin_lock(&netlbl_domhsh_lock); | 335 | spin_lock(&netlbl_domhsh_lock); |
336 | if (entry->valid) { | 336 | if (entry->valid) { |
337 | entry->valid = 0; | 337 | entry->valid = 0; |
338 | if (entry != rcu_dereference(netlbl_domhsh_def)) | 338 | if (entry != rcu_dereference(netlbl_domhsh_def)) |
339 | list_del_rcu(&entry->list); | 339 | list_del_rcu(&entry->list); |
340 | else | 340 | else |
341 | rcu_assign_pointer(netlbl_domhsh_def, NULL); | 341 | rcu_assign_pointer(netlbl_domhsh_def, NULL); |
342 | ret_val = 0; | 342 | ret_val = 0; |
343 | } | 343 | } |
344 | spin_unlock(&netlbl_domhsh_lock); | 344 | spin_unlock(&netlbl_domhsh_lock); |
345 | 345 | ||
346 | audit_buf = netlbl_audit_start_common(AUDIT_MAC_MAP_DEL, audit_info); | 346 | audit_buf = netlbl_audit_start_common(AUDIT_MAC_MAP_DEL, audit_info); |
347 | if (audit_buf != NULL) { | 347 | if (audit_buf != NULL) { |
348 | audit_log_format(audit_buf, | 348 | audit_log_format(audit_buf, |
349 | " nlbl_domain=%s res=%u", | 349 | " nlbl_domain=%s res=%u", |
350 | entry->domain ? entry->domain : "(default)", | 350 | entry->domain ? entry->domain : "(default)", |
351 | ret_val == 0 ? 1 : 0); | 351 | ret_val == 0 ? 1 : 0); |
352 | audit_log_end(audit_buf); | 352 | audit_log_end(audit_buf); |
353 | } | 353 | } |
354 | 354 | ||
355 | remove_return: | 355 | remove_return: |
356 | rcu_read_unlock(); | 356 | rcu_read_unlock(); |
357 | if (ret_val == 0) | 357 | if (ret_val == 0) |
358 | call_rcu(&entry->rcu, netlbl_domhsh_free_entry); | 358 | call_rcu(&entry->rcu, netlbl_domhsh_free_entry); |
359 | return ret_val; | 359 | return ret_val; |
360 | } | 360 | } |
361 | 361 | ||
362 | /** | 362 | /** |
363 | * netlbl_domhsh_remove_default - Removes the default entry from the table | 363 | * netlbl_domhsh_remove_default - Removes the default entry from the table |
364 | * @audit_info: NetLabel audit information | 364 | * @audit_info: NetLabel audit information |
365 | * | 365 | * |
366 | * Description: | 366 | * Description: |
367 | * Removes/resets the default entry for the domain hash table and handles any | 367 | * Removes/resets the default entry for the domain hash table and handles any |
368 | * updates to the lower level protocol handler (i.e. CIPSO). Returns zero on | 368 | * updates to the lower level protocol handler (i.e. CIPSO). Returns zero on |
369 | * success, non-zero on failure. | 369 | * success, non-zero on failure. |
370 | * | 370 | * |
371 | */ | 371 | */ |
372 | int netlbl_domhsh_remove_default(struct netlbl_audit *audit_info) | 372 | int netlbl_domhsh_remove_default(struct netlbl_audit *audit_info) |
373 | { | 373 | { |
374 | return netlbl_domhsh_remove(NULL, audit_info); | 374 | return netlbl_domhsh_remove(NULL, audit_info); |
375 | } | 375 | } |
376 | 376 | ||
377 | /** | 377 | /** |
378 | * netlbl_domhsh_getentry - Get an entry from the domain hash table | 378 | * netlbl_domhsh_getentry - Get an entry from the domain hash table |
379 | * @domain: the domain name to search for | 379 | * @domain: the domain name to search for |
380 | * | 380 | * |
381 | * Description: | 381 | * Description: |
382 | * Look through the domain hash table searching for an entry to match @domain, | 382 | * Look through the domain hash table searching for an entry to match @domain, |
383 | * return a pointer to a copy of the entry or NULL. The caller is responsibile | 383 | * return a pointer to a copy of the entry or NULL. The caller is responsibile |
384 | * for ensuring that rcu_read_[un]lock() is called. | 384 | * for ensuring that rcu_read_[un]lock() is called. |
385 | * | 385 | * |
386 | */ | 386 | */ |
387 | struct netlbl_dom_map *netlbl_domhsh_getentry(const char *domain) | 387 | struct netlbl_dom_map *netlbl_domhsh_getentry(const char *domain) |
388 | { | 388 | { |
389 | return netlbl_domhsh_search_def(domain); | 389 | return netlbl_domhsh_search_def(domain); |
390 | } | 390 | } |
391 | 391 | ||
392 | /** | 392 | /** |
393 | * netlbl_domhsh_walk - Iterate through the domain mapping hash table | 393 | * netlbl_domhsh_walk - Iterate through the domain mapping hash table |
394 | * @skip_bkt: the number of buckets to skip at the start | 394 | * @skip_bkt: the number of buckets to skip at the start |
395 | * @skip_chain: the number of entries to skip in the first iterated bucket | 395 | * @skip_chain: the number of entries to skip in the first iterated bucket |
396 | * @callback: callback for each entry | 396 | * @callback: callback for each entry |
397 | * @cb_arg: argument for the callback function | 397 | * @cb_arg: argument for the callback function |
398 | * | 398 | * |
399 | * Description: | 399 | * Description: |
400 | * Interate over the domain mapping hash table, skipping the first @skip_bkt | 400 | * Interate over the domain mapping hash table, skipping the first @skip_bkt |
401 | * buckets and @skip_chain entries. For each entry in the table call | 401 | * buckets and @skip_chain entries. For each entry in the table call |
402 | * @callback, if @callback returns a negative value stop 'walking' through the | 402 | * @callback, if @callback returns a negative value stop 'walking' through the |
403 | * table and return. Updates the values in @skip_bkt and @skip_chain on | 403 | * table and return. Updates the values in @skip_bkt and @skip_chain on |
404 | * return. Returns zero on succcess, negative values on failure. | 404 | * return. Returns zero on succcess, negative values on failure. |
405 | * | 405 | * |
406 | */ | 406 | */ |
407 | int netlbl_domhsh_walk(u32 *skip_bkt, | 407 | int netlbl_domhsh_walk(u32 *skip_bkt, |
408 | u32 *skip_chain, | 408 | u32 *skip_chain, |
409 | int (*callback) (struct netlbl_dom_map *entry, void *arg), | 409 | int (*callback) (struct netlbl_dom_map *entry, void *arg), |
410 | void *cb_arg) | 410 | void *cb_arg) |
411 | { | 411 | { |
412 | int ret_val = -ENOENT; | 412 | int ret_val = -ENOENT; |
413 | u32 iter_bkt; | 413 | u32 iter_bkt; |
414 | struct netlbl_dom_map *iter_entry; | 414 | struct netlbl_dom_map *iter_entry; |
415 | u32 chain_cnt = 0; | 415 | u32 chain_cnt = 0; |
416 | 416 | ||
417 | rcu_read_lock(); | 417 | rcu_read_lock(); |
418 | for (iter_bkt = *skip_bkt; | 418 | for (iter_bkt = *skip_bkt; |
419 | iter_bkt < rcu_dereference(netlbl_domhsh)->size; | 419 | iter_bkt < rcu_dereference(netlbl_domhsh)->size; |
420 | iter_bkt++, chain_cnt = 0) { | 420 | iter_bkt++, chain_cnt = 0) { |
421 | list_for_each_entry_rcu(iter_entry, | 421 | list_for_each_entry_rcu(iter_entry, |
422 | &rcu_dereference(netlbl_domhsh)->tbl[iter_bkt], | 422 | &rcu_dereference(netlbl_domhsh)->tbl[iter_bkt], |
423 | list) | 423 | list) |
424 | if (iter_entry->valid) { | 424 | if (iter_entry->valid) { |
425 | if (chain_cnt++ < *skip_chain) | 425 | if (chain_cnt++ < *skip_chain) |
426 | continue; | 426 | continue; |
427 | ret_val = callback(iter_entry, cb_arg); | 427 | ret_val = callback(iter_entry, cb_arg); |
428 | if (ret_val < 0) { | 428 | if (ret_val < 0) { |
429 | chain_cnt--; | 429 | chain_cnt--; |
430 | goto walk_return; | 430 | goto walk_return; |
431 | } | 431 | } |
432 | } | 432 | } |
433 | } | 433 | } |
434 | 434 | ||
435 | walk_return: | 435 | walk_return: |
436 | rcu_read_unlock(); | 436 | rcu_read_unlock(); |
437 | *skip_bkt = iter_bkt; | 437 | *skip_bkt = iter_bkt; |
438 | *skip_chain = chain_cnt; | 438 | *skip_chain = chain_cnt; |
439 | return ret_val; | 439 | return ret_val; |
440 | } | 440 | } |
441 | 441 |