blob: d4d664087071a9bbe5c66d948984e4a9ef214938 [file] [log] [blame]
Paul Moored15c3452006-08-03 16:48:37 -07001/*
2 * NetLabel Domain Hash Table
3 *
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
6 * system manages static and dynamic label mappings for network protocols such
7 * as CIPSO and RIPSO.
8 *
Paul Moore82c21bf2011-08-01 11:10:33 +00009 * Author: Paul Moore <paul@paul-moore.com>
Paul Moored15c3452006-08-03 16:48:37 -070010 *
11 */
12
13/*
Paul Moore63c41682008-10-10 10:16:32 -040014 * (c) Copyright Hewlett-Packard Development Company, L.P., 2006, 2008
Paul Moored15c3452006-08-03 16:48:37 -070015 *
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
18 * the Free Software Foundation; either version 2 of the License, or
19 * (at your option) any later version.
20 *
21 * This program is distributed in the hope that it will be useful,
22 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
24 * the GNU General Public License for more details.
25 *
26 * You should have received a copy of the GNU General Public License
Jeff Kirsherd484ff12013-12-06 09:13:41 -080027 * along with this program; if not, see <http://www.gnu.org/licenses/>.
Paul Moored15c3452006-08-03 16:48:37 -070028 *
29 */
30
31#include <linux/types.h>
Franck Bui-Huu82524742008-05-12 21:21:05 +020032#include <linux/rculist.h>
Paul Moored15c3452006-08-03 16:48:37 -070033#include <linux/skbuff.h>
34#include <linux/spinlock.h>
35#include <linux/string.h>
Paul Moore32f50cd2006-09-28 14:51:47 -070036#include <linux/audit.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090037#include <linux/slab.h>
Paul Moored15c3452006-08-03 16:48:37 -070038#include <net/netlabel.h>
39#include <net/cipso_ipv4.h>
40#include <asm/bug.h>
41
42#include "netlabel_mgmt.h"
Paul Moore63c41682008-10-10 10:16:32 -040043#include "netlabel_addrlist.h"
Paul Moored15c3452006-08-03 16:48:37 -070044#include "netlabel_domainhash.h"
Paul Moore32f50cd2006-09-28 14:51:47 -070045#include "netlabel_user.h"
Paul Moored15c3452006-08-03 16:48:37 -070046
47struct netlbl_domhsh_tbl {
48 struct list_head *tbl;
49 u32 size;
50};
51
52/* Domain hash table */
Paul Mooreb914f3a2010-04-01 10:43:57 +000053/* updates should be so rare that having one spinlock for the entire hash table
54 * should be okay */
Adrian Bunk8ce11e62006-08-07 21:50:48 -070055static DEFINE_SPINLOCK(netlbl_domhsh_lock);
Paul Mooreb914f3a2010-04-01 10:43:57 +000056#define netlbl_domhsh_rcu_deref(p) \
Michal Hockod8bf4ca2011-07-08 14:39:41 +020057 rcu_dereference_check(p, lockdep_is_held(&netlbl_domhsh_lock))
Huw Davies96a8f7f2016-06-27 15:02:45 -040058static struct netlbl_domhsh_tbl __rcu *netlbl_domhsh;
59static struct netlbl_dom_map __rcu *netlbl_domhsh_def;
Paul Moored15c3452006-08-03 16:48:37 -070060
61/*
62 * Domain Hash Table Helper Functions
63 */
64
65/**
66 * netlbl_domhsh_free_entry - Frees a domain hash table entry
67 * @entry: the entry's RCU field
68 *
69 * Description:
70 * This function is designed to be used as a callback to the call_rcu()
71 * function so that the memory allocated to a hash table entry can be released
72 * safely.
73 *
74 */
75static void netlbl_domhsh_free_entry(struct rcu_head *entry)
76{
77 struct netlbl_dom_map *ptr;
Paul Moore63c41682008-10-10 10:16:32 -040078 struct netlbl_af4list *iter4;
79 struct netlbl_af4list *tmp4;
Eric Dumazetdfd56b82011-12-10 09:48:31 +000080#if IS_ENABLED(CONFIG_IPV6)
Paul Moore63c41682008-10-10 10:16:32 -040081 struct netlbl_af6list *iter6;
82 struct netlbl_af6list *tmp6;
83#endif /* IPv6 */
Paul Moored15c3452006-08-03 16:48:37 -070084
85 ptr = container_of(entry, struct netlbl_dom_map, rcu);
Paul Moore6a8b7f02013-08-02 14:45:08 -040086 if (ptr->def.type == NETLBL_NLTYPE_ADDRSELECT) {
Paul Moore63c41682008-10-10 10:16:32 -040087 netlbl_af4list_foreach_safe(iter4, tmp4,
Paul Moore6a8b7f02013-08-02 14:45:08 -040088 &ptr->def.addrsel->list4) {
Paul Moore63c41682008-10-10 10:16:32 -040089 netlbl_af4list_remove_entry(iter4);
90 kfree(netlbl_domhsh_addr4_entry(iter4));
91 }
Eric Dumazetdfd56b82011-12-10 09:48:31 +000092#if IS_ENABLED(CONFIG_IPV6)
Paul Moore63c41682008-10-10 10:16:32 -040093 netlbl_af6list_foreach_safe(iter6, tmp6,
Paul Moore6a8b7f02013-08-02 14:45:08 -040094 &ptr->def.addrsel->list6) {
Paul Moore63c41682008-10-10 10:16:32 -040095 netlbl_af6list_remove_entry(iter6);
96 kfree(netlbl_domhsh_addr6_entry(iter6));
97 }
98#endif /* IPv6 */
99 }
Paul Moored15c3452006-08-03 16:48:37 -0700100 kfree(ptr->domain);
101 kfree(ptr);
102}
103
104/**
105 * netlbl_domhsh_hash - Hashing function for the domain hash table
106 * @domain: the domain name to hash
107 *
108 * Description:
109 * This is the hashing function for the domain hash table, it returns the
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300110 * correct bucket number for the domain. The caller is responsible for
Paul Mooreb914f3a2010-04-01 10:43:57 +0000111 * ensuring that the hash table is protected with either a RCU read lock or the
112 * hash table lock.
Paul Moored15c3452006-08-03 16:48:37 -0700113 *
114 */
115static u32 netlbl_domhsh_hash(const char *key)
116{
117 u32 iter;
118 u32 val;
119 u32 len;
120
121 /* This is taken (with slight modification) from
122 * security/selinux/ss/symtab.c:symhash() */
123
124 for (iter = 0, val = 0, len = strlen(key); iter < len; iter++)
125 val = (val << 4 | (val >> (8 * sizeof(u32) - 4))) ^ key[iter];
Paul Mooreb914f3a2010-04-01 10:43:57 +0000126 return val & (netlbl_domhsh_rcu_deref(netlbl_domhsh)->size - 1);
Paul Moored15c3452006-08-03 16:48:37 -0700127}
128
129/**
130 * netlbl_domhsh_search - Search for a domain entry
131 * @domain: the domain
Paul Moored15c3452006-08-03 16:48:37 -0700132 *
133 * Description:
134 * Searches the domain hash table and returns a pointer to the hash table
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300135 * entry if found, otherwise NULL is returned. The caller is responsible for
Paul Mooreb914f3a2010-04-01 10:43:57 +0000136 * ensuring that the hash table is protected with either a RCU read lock or the
137 * hash table lock.
Paul Moored15c3452006-08-03 16:48:37 -0700138 *
139 */
Paul Mooreb64397e2008-01-29 08:37:54 -0500140static struct netlbl_dom_map *netlbl_domhsh_search(const char *domain)
Paul Moored15c3452006-08-03 16:48:37 -0700141{
142 u32 bkt;
Paul Moore56196702008-10-10 10:16:29 -0400143 struct list_head *bkt_list;
Paul Moored15c3452006-08-03 16:48:37 -0700144 struct netlbl_dom_map *iter;
145
146 if (domain != NULL) {
147 bkt = netlbl_domhsh_hash(domain);
Paul Mooreb914f3a2010-04-01 10:43:57 +0000148 bkt_list = &netlbl_domhsh_rcu_deref(netlbl_domhsh)->tbl[bkt];
Paul Moore56196702008-10-10 10:16:29 -0400149 list_for_each_entry_rcu(iter, bkt_list, list)
Paul Moored15c3452006-08-03 16:48:37 -0700150 if (iter->valid && strcmp(iter->domain, domain) == 0)
151 return iter;
152 }
153
Paul Mooreb64397e2008-01-29 08:37:54 -0500154 return NULL;
155}
156
157/**
158 * netlbl_domhsh_search_def - Search for a domain entry
159 * @domain: the domain
160 * @def: return default if no match is found
161 *
162 * Description:
163 * Searches the domain hash table and returns a pointer to the hash table
164 * entry if an exact match is found, if an exact match is not present in the
165 * hash table then the default entry is returned if valid otherwise NULL is
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300166 * returned. The caller is responsible ensuring that the hash table is
Paul Mooreb914f3a2010-04-01 10:43:57 +0000167 * protected with either a RCU read lock or the hash table lock.
Paul Mooreb64397e2008-01-29 08:37:54 -0500168 *
169 */
170static struct netlbl_dom_map *netlbl_domhsh_search_def(const char *domain)
171{
172 struct netlbl_dom_map *entry;
173
174 entry = netlbl_domhsh_search(domain);
175 if (entry == NULL) {
Paul Mooreb914f3a2010-04-01 10:43:57 +0000176 entry = netlbl_domhsh_rcu_deref(netlbl_domhsh_def);
Pavel Emelyanov4c3a0a22008-02-12 22:15:14 -0800177 if (entry != NULL && !entry->valid)
178 entry = NULL;
Paul Moored15c3452006-08-03 16:48:37 -0700179 }
180
Pavel Emelyanov4c3a0a22008-02-12 22:15:14 -0800181 return entry;
Paul Moored15c3452006-08-03 16:48:37 -0700182}
183
Paul Moore63c41682008-10-10 10:16:32 -0400184/**
185 * netlbl_domhsh_audit_add - Generate an audit entry for an add event
186 * @entry: the entry being added
187 * @addr4: the IPv4 address information
188 * @addr6: the IPv6 address information
189 * @result: the result code
190 * @audit_info: NetLabel audit information
191 *
192 * Description:
193 * Generate an audit record for adding a new NetLabel/LSM mapping entry with
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300194 * the given information. Caller is responsible for holding the necessary
Paul Moore63c41682008-10-10 10:16:32 -0400195 * locks.
196 *
197 */
198static void netlbl_domhsh_audit_add(struct netlbl_dom_map *entry,
199 struct netlbl_af4list *addr4,
200 struct netlbl_af6list *addr6,
201 int result,
202 struct netlbl_audit *audit_info)
203{
204 struct audit_buffer *audit_buf;
205 struct cipso_v4_doi *cipsov4 = NULL;
206 u32 type;
207
208 audit_buf = netlbl_audit_start_common(AUDIT_MAC_MAP_ADD, audit_info);
209 if (audit_buf != NULL) {
210 audit_log_format(audit_buf, " nlbl_domain=%s",
211 entry->domain ? entry->domain : "(default)");
212 if (addr4 != NULL) {
213 struct netlbl_domaddr4_map *map4;
214 map4 = netlbl_domhsh_addr4_entry(addr4);
Paul Moore6a8b7f02013-08-02 14:45:08 -0400215 type = map4->def.type;
216 cipsov4 = map4->def.cipso;
Paul Moore63c41682008-10-10 10:16:32 -0400217 netlbl_af4list_audit_addr(audit_buf, 0, NULL,
218 addr4->addr, addr4->mask);
Eric Dumazetdfd56b82011-12-10 09:48:31 +0000219#if IS_ENABLED(CONFIG_IPV6)
Paul Moore63c41682008-10-10 10:16:32 -0400220 } else if (addr6 != NULL) {
221 struct netlbl_domaddr6_map *map6;
222 map6 = netlbl_domhsh_addr6_entry(addr6);
Paul Moore6a8b7f02013-08-02 14:45:08 -0400223 type = map6->def.type;
Paul Moore63c41682008-10-10 10:16:32 -0400224 netlbl_af6list_audit_addr(audit_buf, 0, NULL,
225 &addr6->addr, &addr6->mask);
226#endif /* IPv6 */
227 } else {
Paul Moore6a8b7f02013-08-02 14:45:08 -0400228 type = entry->def.type;
229 cipsov4 = entry->def.cipso;
Paul Moore63c41682008-10-10 10:16:32 -0400230 }
231 switch (type) {
232 case NETLBL_NLTYPE_UNLABELED:
233 audit_log_format(audit_buf, " nlbl_protocol=unlbl");
234 break;
235 case NETLBL_NLTYPE_CIPSOV4:
236 BUG_ON(cipsov4 == NULL);
237 audit_log_format(audit_buf,
238 " nlbl_protocol=cipsov4 cipso_doi=%u",
239 cipsov4->doi);
240 break;
241 }
242 audit_log_format(audit_buf, " res=%u", result == 0 ? 1 : 0);
243 audit_log_end(audit_buf);
244 }
245}
246
Paul Moore6b21e1b2013-05-17 09:08:50 +0000247/**
248 * netlbl_domhsh_validate - Validate a new domain mapping entry
249 * @entry: the entry to validate
250 *
251 * This function validates the new domain mapping entry to ensure that it is
252 * a valid entry. Returns zero on success, negative values on failure.
253 *
254 */
255static int netlbl_domhsh_validate(const struct netlbl_dom_map *entry)
256{
257 struct netlbl_af4list *iter4;
258 struct netlbl_domaddr4_map *map4;
259#if IS_ENABLED(CONFIG_IPV6)
260 struct netlbl_af6list *iter6;
261 struct netlbl_domaddr6_map *map6;
262#endif /* IPv6 */
263
264 if (entry == NULL)
265 return -EINVAL;
266
Paul Moore6a8b7f02013-08-02 14:45:08 -0400267 switch (entry->def.type) {
Paul Moore6b21e1b2013-05-17 09:08:50 +0000268 case NETLBL_NLTYPE_UNLABELED:
Paul Moore6a8b7f02013-08-02 14:45:08 -0400269 if (entry->def.cipso != NULL || entry->def.addrsel != NULL)
Paul Moore6b21e1b2013-05-17 09:08:50 +0000270 return -EINVAL;
271 break;
272 case NETLBL_NLTYPE_CIPSOV4:
Paul Moore6a8b7f02013-08-02 14:45:08 -0400273 if (entry->def.cipso == NULL)
Paul Moore6b21e1b2013-05-17 09:08:50 +0000274 return -EINVAL;
275 break;
276 case NETLBL_NLTYPE_ADDRSELECT:
Paul Moore6a8b7f02013-08-02 14:45:08 -0400277 netlbl_af4list_foreach(iter4, &entry->def.addrsel->list4) {
Paul Moore6b21e1b2013-05-17 09:08:50 +0000278 map4 = netlbl_domhsh_addr4_entry(iter4);
Paul Moore6a8b7f02013-08-02 14:45:08 -0400279 switch (map4->def.type) {
Paul Moore6b21e1b2013-05-17 09:08:50 +0000280 case NETLBL_NLTYPE_UNLABELED:
Paul Moore6a8b7f02013-08-02 14:45:08 -0400281 if (map4->def.cipso != NULL)
Paul Moore6b21e1b2013-05-17 09:08:50 +0000282 return -EINVAL;
283 break;
284 case NETLBL_NLTYPE_CIPSOV4:
Paul Moore6a8b7f02013-08-02 14:45:08 -0400285 if (map4->def.cipso == NULL)
Paul Moore6b21e1b2013-05-17 09:08:50 +0000286 return -EINVAL;
287 break;
288 default:
289 return -EINVAL;
290 }
291 }
292#if IS_ENABLED(CONFIG_IPV6)
Paul Moore6a8b7f02013-08-02 14:45:08 -0400293 netlbl_af6list_foreach(iter6, &entry->def.addrsel->list6) {
Paul Moore6b21e1b2013-05-17 09:08:50 +0000294 map6 = netlbl_domhsh_addr6_entry(iter6);
Paul Moore6a8b7f02013-08-02 14:45:08 -0400295 switch (map6->def.type) {
Paul Moore6b21e1b2013-05-17 09:08:50 +0000296 case NETLBL_NLTYPE_UNLABELED:
297 break;
298 default:
299 return -EINVAL;
300 }
301 }
302#endif /* IPv6 */
303 break;
304 default:
305 return -EINVAL;
306 }
307
308 return 0;
309}
310
Paul Moored15c3452006-08-03 16:48:37 -0700311/*
312 * Domain Hash Table Functions
313 */
314
315/**
316 * netlbl_domhsh_init - Init for the domain hash
317 * @size: the number of bits to use for the hash buckets
318 *
319 * Description:
320 * Initializes the domain hash table, should be called only by
321 * netlbl_user_init() during initialization. Returns zero on success, non-zero
322 * values on error.
323 *
324 */
Pavel Emelyanov05705e42008-02-17 22:33:57 -0800325int __init netlbl_domhsh_init(u32 size)
Paul Moored15c3452006-08-03 16:48:37 -0700326{
327 u32 iter;
328 struct netlbl_domhsh_tbl *hsh_tbl;
329
330 if (size == 0)
331 return -EINVAL;
332
333 hsh_tbl = kmalloc(sizeof(*hsh_tbl), GFP_KERNEL);
334 if (hsh_tbl == NULL)
335 return -ENOMEM;
336 hsh_tbl->size = 1 << size;
337 hsh_tbl->tbl = kcalloc(hsh_tbl->size,
338 sizeof(struct list_head),
339 GFP_KERNEL);
340 if (hsh_tbl->tbl == NULL) {
341 kfree(hsh_tbl);
342 return -ENOMEM;
343 }
344 for (iter = 0; iter < hsh_tbl->size; iter++)
345 INIT_LIST_HEAD(&hsh_tbl->tbl[iter]);
346
Paul Moored15c3452006-08-03 16:48:37 -0700347 spin_lock(&netlbl_domhsh_lock);
Eric Dumazetcf778b02012-01-12 04:41:32 +0000348 rcu_assign_pointer(netlbl_domhsh, hsh_tbl);
Paul Moored15c3452006-08-03 16:48:37 -0700349 spin_unlock(&netlbl_domhsh_lock);
Paul Moored15c3452006-08-03 16:48:37 -0700350
351 return 0;
352}
353
354/**
355 * netlbl_domhsh_add - Adds a entry to the domain hash table
356 * @entry: the entry to add
Paul Moore95d4e6b2006-09-29 17:05:05 -0700357 * @audit_info: NetLabel audit information
Paul Moored15c3452006-08-03 16:48:37 -0700358 *
359 * Description:
360 * Adds a new entry to the domain hash table and handles any updates to the
361 * lower level protocol handler (i.e. CIPSO). Returns zero on success,
362 * negative on failure.
363 *
364 */
Paul Moore95d4e6b2006-09-29 17:05:05 -0700365int netlbl_domhsh_add(struct netlbl_dom_map *entry,
366 struct netlbl_audit *audit_info)
Paul Moored15c3452006-08-03 16:48:37 -0700367{
Paul Moore63c41682008-10-10 10:16:32 -0400368 int ret_val = 0;
369 struct netlbl_dom_map *entry_old;
370 struct netlbl_af4list *iter4;
371 struct netlbl_af4list *tmp4;
Eric Dumazetdfd56b82011-12-10 09:48:31 +0000372#if IS_ENABLED(CONFIG_IPV6)
Paul Moore63c41682008-10-10 10:16:32 -0400373 struct netlbl_af6list *iter6;
374 struct netlbl_af6list *tmp6;
375#endif /* IPv6 */
Paul Moored15c3452006-08-03 16:48:37 -0700376
Paul Moore6b21e1b2013-05-17 09:08:50 +0000377 ret_val = netlbl_domhsh_validate(entry);
378 if (ret_val != 0)
379 return ret_val;
380
Paul Mooreb914f3a2010-04-01 10:43:57 +0000381 /* XXX - we can remove this RCU read lock as the spinlock protects the
382 * entire function, but before we do we need to fixup the
383 * netlbl_af[4,6]list RCU functions to do "the right thing" with
384 * respect to rcu_dereference() when only a spinlock is held. */
Paul Moored15c3452006-08-03 16:48:37 -0700385 rcu_read_lock();
Paul Moore1c3fad92008-01-29 08:37:57 -0500386 spin_lock(&netlbl_domhsh_lock);
Paul Moore63c41682008-10-10 10:16:32 -0400387 if (entry->domain != NULL)
388 entry_old = netlbl_domhsh_search(entry->domain);
389 else
390 entry_old = netlbl_domhsh_search_def(entry->domain);
391 if (entry_old == NULL) {
392 entry->valid = 1;
Paul Moore63c41682008-10-10 10:16:32 -0400393
394 if (entry->domain != NULL) {
395 u32 bkt = netlbl_domhsh_hash(entry->domain);
Paul Moored15c3452006-08-03 16:48:37 -0700396 list_add_tail_rcu(&entry->list,
Paul Moore3482fd92007-08-07 17:53:10 -0700397 &rcu_dereference(netlbl_domhsh)->tbl[bkt]);
Paul Moore63c41682008-10-10 10:16:32 -0400398 } else {
399 INIT_LIST_HEAD(&entry->list);
Eric Dumazetcf778b02012-01-12 04:41:32 +0000400 rcu_assign_pointer(netlbl_domhsh_def, entry);
Paul Moorede646882006-11-17 17:38:55 -0500401 }
Paul Moored15c3452006-08-03 16:48:37 -0700402
Paul Moore6a8b7f02013-08-02 14:45:08 -0400403 if (entry->def.type == NETLBL_NLTYPE_ADDRSELECT) {
Paul Moore63c41682008-10-10 10:16:32 -0400404 netlbl_af4list_foreach_rcu(iter4,
Paul Moore6a8b7f02013-08-02 14:45:08 -0400405 &entry->def.addrsel->list4)
Paul Moore63c41682008-10-10 10:16:32 -0400406 netlbl_domhsh_audit_add(entry, iter4, NULL,
407 ret_val, audit_info);
Eric Dumazetdfd56b82011-12-10 09:48:31 +0000408#if IS_ENABLED(CONFIG_IPV6)
Paul Moore63c41682008-10-10 10:16:32 -0400409 netlbl_af6list_foreach_rcu(iter6,
Paul Moore6a8b7f02013-08-02 14:45:08 -0400410 &entry->def.addrsel->list6)
Paul Moore63c41682008-10-10 10:16:32 -0400411 netlbl_domhsh_audit_add(entry, NULL, iter6,
412 ret_val, audit_info);
413#endif /* IPv6 */
414 } else
415 netlbl_domhsh_audit_add(entry, NULL, NULL,
416 ret_val, audit_info);
Paul Moore6a8b7f02013-08-02 14:45:08 -0400417 } else if (entry_old->def.type == NETLBL_NLTYPE_ADDRSELECT &&
418 entry->def.type == NETLBL_NLTYPE_ADDRSELECT) {
Paul Moore63c41682008-10-10 10:16:32 -0400419 struct list_head *old_list4;
420 struct list_head *old_list6;
421
Paul Moore6a8b7f02013-08-02 14:45:08 -0400422 old_list4 = &entry_old->def.addrsel->list4;
423 old_list6 = &entry_old->def.addrsel->list6;
Paul Moore63c41682008-10-10 10:16:32 -0400424
425 /* we only allow the addition of address selectors if all of
426 * the selectors do not exist in the existing domain map */
Paul Moore6a8b7f02013-08-02 14:45:08 -0400427 netlbl_af4list_foreach_rcu(iter4, &entry->def.addrsel->list4)
Paul Moore63c41682008-10-10 10:16:32 -0400428 if (netlbl_af4list_search_exact(iter4->addr,
429 iter4->mask,
430 old_list4)) {
431 ret_val = -EEXIST;
432 goto add_return;
433 }
Eric Dumazetdfd56b82011-12-10 09:48:31 +0000434#if IS_ENABLED(CONFIG_IPV6)
Paul Moore6a8b7f02013-08-02 14:45:08 -0400435 netlbl_af6list_foreach_rcu(iter6, &entry->def.addrsel->list6)
Paul Moore63c41682008-10-10 10:16:32 -0400436 if (netlbl_af6list_search_exact(&iter6->addr,
437 &iter6->mask,
438 old_list6)) {
439 ret_val = -EEXIST;
440 goto add_return;
441 }
442#endif /* IPv6 */
443
444 netlbl_af4list_foreach_safe(iter4, tmp4,
Paul Moore6a8b7f02013-08-02 14:45:08 -0400445 &entry->def.addrsel->list4) {
Paul Moore63c41682008-10-10 10:16:32 -0400446 netlbl_af4list_remove_entry(iter4);
447 iter4->valid = 1;
448 ret_val = netlbl_af4list_add(iter4, old_list4);
449 netlbl_domhsh_audit_add(entry_old, iter4, NULL,
450 ret_val, audit_info);
451 if (ret_val != 0)
452 goto add_return;
453 }
Eric Dumazetdfd56b82011-12-10 09:48:31 +0000454#if IS_ENABLED(CONFIG_IPV6)
Paul Moore63c41682008-10-10 10:16:32 -0400455 netlbl_af6list_foreach_safe(iter6, tmp6,
Paul Moore6a8b7f02013-08-02 14:45:08 -0400456 &entry->def.addrsel->list6) {
Paul Moore63c41682008-10-10 10:16:32 -0400457 netlbl_af6list_remove_entry(iter6);
458 iter6->valid = 1;
459 ret_val = netlbl_af6list_add(iter6, old_list6);
460 netlbl_domhsh_audit_add(entry_old, NULL, iter6,
461 ret_val, audit_info);
462 if (ret_val != 0)
463 goto add_return;
464 }
465#endif /* IPv6 */
466 } else
467 ret_val = -EINVAL;
468
469add_return:
470 spin_unlock(&netlbl_domhsh_lock);
471 rcu_read_unlock();
Paul Moored15c3452006-08-03 16:48:37 -0700472 return ret_val;
473}
474
475/**
476 * netlbl_domhsh_add_default - Adds the default entry to the domain hash table
477 * @entry: the entry to add
Paul Moore95d4e6b2006-09-29 17:05:05 -0700478 * @audit_info: NetLabel audit information
Paul Moored15c3452006-08-03 16:48:37 -0700479 *
480 * Description:
481 * Adds a new default entry to the domain hash table and handles any updates
482 * to the lower level protocol handler (i.e. CIPSO). Returns zero on success,
483 * negative on failure.
484 *
485 */
Paul Moore95d4e6b2006-09-29 17:05:05 -0700486int netlbl_domhsh_add_default(struct netlbl_dom_map *entry,
487 struct netlbl_audit *audit_info)
Paul Moored15c3452006-08-03 16:48:37 -0700488{
Paul Moore95d4e6b2006-09-29 17:05:05 -0700489 return netlbl_domhsh_add(entry, audit_info);
Paul Moored15c3452006-08-03 16:48:37 -0700490}
491
492/**
Paul Mooreb1edeb12008-10-10 10:16:31 -0400493 * netlbl_domhsh_remove_entry - Removes a given entry from the domain table
494 * @entry: the entry to remove
495 * @audit_info: NetLabel audit information
496 *
497 * Description:
498 * Removes an entry from the domain hash table and handles any updates to the
499 * lower level protocol handler (i.e. CIPSO). Caller is responsible for
500 * ensuring that the RCU read lock is held. Returns zero on success, negative
501 * on failure.
502 *
503 */
504int netlbl_domhsh_remove_entry(struct netlbl_dom_map *entry,
505 struct netlbl_audit *audit_info)
506{
507 int ret_val = 0;
508 struct audit_buffer *audit_buf;
509
510 if (entry == NULL)
511 return -ENOENT;
512
513 spin_lock(&netlbl_domhsh_lock);
514 if (entry->valid) {
515 entry->valid = 0;
516 if (entry != rcu_dereference(netlbl_domhsh_def))
517 list_del_rcu(&entry->list);
518 else
Stephen Hemmingera9b3cd72011-08-01 16:19:00 +0000519 RCU_INIT_POINTER(netlbl_domhsh_def, NULL);
Paul Mooreb1edeb12008-10-10 10:16:31 -0400520 } else
521 ret_val = -ENOENT;
522 spin_unlock(&netlbl_domhsh_lock);
523
524 audit_buf = netlbl_audit_start_common(AUDIT_MAC_MAP_DEL, audit_info);
525 if (audit_buf != NULL) {
526 audit_log_format(audit_buf,
527 " nlbl_domain=%s res=%u",
528 entry->domain ? entry->domain : "(default)",
529 ret_val == 0 ? 1 : 0);
530 audit_log_end(audit_buf);
531 }
532
533 if (ret_val == 0) {
Paul Moore63c41682008-10-10 10:16:32 -0400534 struct netlbl_af4list *iter4;
535 struct netlbl_domaddr4_map *map4;
536
Paul Moore6a8b7f02013-08-02 14:45:08 -0400537 switch (entry->def.type) {
Paul Moore63c41682008-10-10 10:16:32 -0400538 case NETLBL_NLTYPE_ADDRSELECT:
539 netlbl_af4list_foreach_rcu(iter4,
Paul Moore6a8b7f02013-08-02 14:45:08 -0400540 &entry->def.addrsel->list4) {
Paul Moore63c41682008-10-10 10:16:32 -0400541 map4 = netlbl_domhsh_addr4_entry(iter4);
Paul Moore6a8b7f02013-08-02 14:45:08 -0400542 cipso_v4_doi_putdef(map4->def.cipso);
Paul Moore63c41682008-10-10 10:16:32 -0400543 }
544 /* no need to check the IPv6 list since we currently
545 * support only unlabeled protocols for IPv6 */
546 break;
Paul Mooreb1edeb12008-10-10 10:16:31 -0400547 case NETLBL_NLTYPE_CIPSOV4:
Paul Moore6a8b7f02013-08-02 14:45:08 -0400548 cipso_v4_doi_putdef(entry->def.cipso);
Paul Mooreb1edeb12008-10-10 10:16:31 -0400549 break;
550 }
551 call_rcu(&entry->rcu, netlbl_domhsh_free_entry);
552 }
553
554 return ret_val;
555}
556
557/**
Paul Moore6c2e8ac2008-12-31 12:54:11 -0500558 * netlbl_domhsh_remove_af4 - Removes an address selector entry
559 * @domain: the domain
560 * @addr: IPv4 address
561 * @mask: IPv4 address mask
562 * @audit_info: NetLabel audit information
563 *
564 * Description:
565 * Removes an individual address selector from a domain mapping and potentially
566 * the entire mapping if it is empty. Returns zero on success, negative values
567 * on failure.
568 *
569 */
570int netlbl_domhsh_remove_af4(const char *domain,
571 const struct in_addr *addr,
572 const struct in_addr *mask,
573 struct netlbl_audit *audit_info)
574{
575 struct netlbl_dom_map *entry_map;
576 struct netlbl_af4list *entry_addr;
577 struct netlbl_af4list *iter4;
Eric Dumazetdfd56b82011-12-10 09:48:31 +0000578#if IS_ENABLED(CONFIG_IPV6)
Paul Moore6c2e8ac2008-12-31 12:54:11 -0500579 struct netlbl_af6list *iter6;
580#endif /* IPv6 */
581 struct netlbl_domaddr4_map *entry;
582
583 rcu_read_lock();
584
585 if (domain)
586 entry_map = netlbl_domhsh_search(domain);
587 else
588 entry_map = netlbl_domhsh_search_def(domain);
Paul Moore6a8b7f02013-08-02 14:45:08 -0400589 if (entry_map == NULL ||
590 entry_map->def.type != NETLBL_NLTYPE_ADDRSELECT)
Paul Moore6c2e8ac2008-12-31 12:54:11 -0500591 goto remove_af4_failure;
592
593 spin_lock(&netlbl_domhsh_lock);
594 entry_addr = netlbl_af4list_remove(addr->s_addr, mask->s_addr,
Paul Moore6a8b7f02013-08-02 14:45:08 -0400595 &entry_map->def.addrsel->list4);
Paul Moore6c2e8ac2008-12-31 12:54:11 -0500596 spin_unlock(&netlbl_domhsh_lock);
597
598 if (entry_addr == NULL)
599 goto remove_af4_failure;
Paul Moore6a8b7f02013-08-02 14:45:08 -0400600 netlbl_af4list_foreach_rcu(iter4, &entry_map->def.addrsel->list4)
Paul Moore6c2e8ac2008-12-31 12:54:11 -0500601 goto remove_af4_single_addr;
Eric Dumazetdfd56b82011-12-10 09:48:31 +0000602#if IS_ENABLED(CONFIG_IPV6)
Paul Moore6a8b7f02013-08-02 14:45:08 -0400603 netlbl_af6list_foreach_rcu(iter6, &entry_map->def.addrsel->list6)
Paul Moore6c2e8ac2008-12-31 12:54:11 -0500604 goto remove_af4_single_addr;
605#endif /* IPv6 */
606 /* the domain mapping is empty so remove it from the mapping table */
607 netlbl_domhsh_remove_entry(entry_map, audit_info);
608
609remove_af4_single_addr:
610 rcu_read_unlock();
611 /* yick, we can't use call_rcu here because we don't have a rcu head
612 * pointer but hopefully this should be a rare case so the pause
613 * shouldn't be a problem */
614 synchronize_rcu();
615 entry = netlbl_domhsh_addr4_entry(entry_addr);
Paul Moore6a8b7f02013-08-02 14:45:08 -0400616 cipso_v4_doi_putdef(entry->def.cipso);
Paul Moore6c2e8ac2008-12-31 12:54:11 -0500617 kfree(entry);
618 return 0;
619
620remove_af4_failure:
621 rcu_read_unlock();
622 return -ENOENT;
623}
624
625/**
Paul Moored15c3452006-08-03 16:48:37 -0700626 * netlbl_domhsh_remove - Removes an entry from the domain hash table
627 * @domain: the domain to remove
Paul Moore95d4e6b2006-09-29 17:05:05 -0700628 * @audit_info: NetLabel audit information
Paul Moored15c3452006-08-03 16:48:37 -0700629 *
630 * Description:
631 * Removes an entry from the domain hash table and handles any updates to the
632 * lower level protocol handler (i.e. CIPSO). Returns zero on success,
633 * negative on failure.
634 *
635 */
Paul Moore95d4e6b2006-09-29 17:05:05 -0700636int netlbl_domhsh_remove(const char *domain, struct netlbl_audit *audit_info)
Paul Moored15c3452006-08-03 16:48:37 -0700637{
Paul Mooreb1edeb12008-10-10 10:16:31 -0400638 int ret_val;
Paul Moored15c3452006-08-03 16:48:37 -0700639 struct netlbl_dom_map *entry;
640
641 rcu_read_lock();
Paul Mooreb64397e2008-01-29 08:37:54 -0500642 if (domain)
643 entry = netlbl_domhsh_search(domain);
644 else
645 entry = netlbl_domhsh_search_def(domain);
Paul Mooreb1edeb12008-10-10 10:16:31 -0400646 ret_val = netlbl_domhsh_remove_entry(entry, audit_info);
Paul Moored15c3452006-08-03 16:48:37 -0700647 rcu_read_unlock();
Paul Mooreb1edeb12008-10-10 10:16:31 -0400648
Paul Moored15c3452006-08-03 16:48:37 -0700649 return ret_val;
650}
651
652/**
653 * netlbl_domhsh_remove_default - Removes the default entry from the table
Paul Moore95d4e6b2006-09-29 17:05:05 -0700654 * @audit_info: NetLabel audit information
Paul Moored15c3452006-08-03 16:48:37 -0700655 *
656 * Description:
657 * Removes/resets the default entry for the domain hash table and handles any
658 * updates to the lower level protocol handler (i.e. CIPSO). Returns zero on
659 * success, non-zero on failure.
660 *
661 */
Paul Moore95d4e6b2006-09-29 17:05:05 -0700662int netlbl_domhsh_remove_default(struct netlbl_audit *audit_info)
Paul Moored15c3452006-08-03 16:48:37 -0700663{
Paul Moore95d4e6b2006-09-29 17:05:05 -0700664 return netlbl_domhsh_remove(NULL, audit_info);
Paul Moored15c3452006-08-03 16:48:37 -0700665}
666
667/**
668 * netlbl_domhsh_getentry - Get an entry from the domain hash table
669 * @domain: the domain name to search for
670 *
671 * Description:
672 * Look through the domain hash table searching for an entry to match @domain,
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300673 * return a pointer to a copy of the entry or NULL. The caller is responsible
Paul Moored15c3452006-08-03 16:48:37 -0700674 * for ensuring that rcu_read_[un]lock() is called.
675 *
676 */
677struct netlbl_dom_map *netlbl_domhsh_getentry(const char *domain)
678{
Paul Mooreb64397e2008-01-29 08:37:54 -0500679 return netlbl_domhsh_search_def(domain);
Paul Moored15c3452006-08-03 16:48:37 -0700680}
681
682/**
Paul Moore63c41682008-10-10 10:16:32 -0400683 * netlbl_domhsh_getentry_af4 - Get an entry from the domain hash table
684 * @domain: the domain name to search for
685 * @addr: the IP address to search for
686 *
687 * Description:
688 * Look through the domain hash table searching for an entry to match @domain
689 * and @addr, return a pointer to a copy of the entry or NULL. The caller is
690 * responsible for ensuring that rcu_read_[un]lock() is called.
691 *
692 */
Paul Moore6a8b7f02013-08-02 14:45:08 -0400693struct netlbl_dommap_def *netlbl_domhsh_getentry_af4(const char *domain,
694 __be32 addr)
Paul Moore63c41682008-10-10 10:16:32 -0400695{
696 struct netlbl_dom_map *dom_iter;
697 struct netlbl_af4list *addr_iter;
698
699 dom_iter = netlbl_domhsh_search_def(domain);
700 if (dom_iter == NULL)
701 return NULL;
Paul Moore63c41682008-10-10 10:16:32 -0400702
Paul Moore6a8b7f02013-08-02 14:45:08 -0400703 if (dom_iter->def.type != NETLBL_NLTYPE_ADDRSELECT)
704 return &dom_iter->def;
705 addr_iter = netlbl_af4list_search(addr, &dom_iter->def.addrsel->list4);
Paul Moore63c41682008-10-10 10:16:32 -0400706 if (addr_iter == NULL)
707 return NULL;
Paul Moore6a8b7f02013-08-02 14:45:08 -0400708 return &(netlbl_domhsh_addr4_entry(addr_iter)->def);
Paul Moore63c41682008-10-10 10:16:32 -0400709}
710
Eric Dumazetdfd56b82011-12-10 09:48:31 +0000711#if IS_ENABLED(CONFIG_IPV6)
Paul Moore63c41682008-10-10 10:16:32 -0400712/**
713 * netlbl_domhsh_getentry_af6 - Get an entry from the domain hash table
714 * @domain: the domain name to search for
715 * @addr: the IP address to search for
716 *
717 * Description:
718 * Look through the domain hash table searching for an entry to match @domain
719 * and @addr, return a pointer to a copy of the entry or NULL. The caller is
720 * responsible for ensuring that rcu_read_[un]lock() is called.
721 *
722 */
Paul Moore6a8b7f02013-08-02 14:45:08 -0400723struct netlbl_dommap_def *netlbl_domhsh_getentry_af6(const char *domain,
Paul Moore63c41682008-10-10 10:16:32 -0400724 const struct in6_addr *addr)
725{
726 struct netlbl_dom_map *dom_iter;
727 struct netlbl_af6list *addr_iter;
728
729 dom_iter = netlbl_domhsh_search_def(domain);
730 if (dom_iter == NULL)
731 return NULL;
Paul Moore63c41682008-10-10 10:16:32 -0400732
Paul Moore6a8b7f02013-08-02 14:45:08 -0400733 if (dom_iter->def.type != NETLBL_NLTYPE_ADDRSELECT)
734 return &dom_iter->def;
735 addr_iter = netlbl_af6list_search(addr, &dom_iter->def.addrsel->list6);
Paul Moore63c41682008-10-10 10:16:32 -0400736 if (addr_iter == NULL)
737 return NULL;
Paul Moore6a8b7f02013-08-02 14:45:08 -0400738 return &(netlbl_domhsh_addr6_entry(addr_iter)->def);
Paul Moore63c41682008-10-10 10:16:32 -0400739}
740#endif /* IPv6 */
741
742/**
Paul Moorefcd48282006-09-25 15:56:09 -0700743 * netlbl_domhsh_walk - Iterate through the domain mapping hash table
744 * @skip_bkt: the number of buckets to skip at the start
745 * @skip_chain: the number of entries to skip in the first iterated bucket
746 * @callback: callback for each entry
747 * @cb_arg: argument for the callback function
Paul Moored15c3452006-08-03 16:48:37 -0700748 *
749 * Description:
Paul Moorefcd48282006-09-25 15:56:09 -0700750 * Interate over the domain mapping hash table, skipping the first @skip_bkt
751 * buckets and @skip_chain entries. For each entry in the table call
752 * @callback, if @callback returns a negative value stop 'walking' through the
753 * table and return. Updates the values in @skip_bkt and @skip_chain on
André Goddard Rosaaf901ca2009-11-14 13:09:05 -0200754 * return. Returns zero on success, negative values on failure.
Paul Moored15c3452006-08-03 16:48:37 -0700755 *
756 */
Paul Moorefcd48282006-09-25 15:56:09 -0700757int netlbl_domhsh_walk(u32 *skip_bkt,
758 u32 *skip_chain,
759 int (*callback) (struct netlbl_dom_map *entry, void *arg),
760 void *cb_arg)
Paul Moored15c3452006-08-03 16:48:37 -0700761{
Paul Moorefcd48282006-09-25 15:56:09 -0700762 int ret_val = -ENOENT;
763 u32 iter_bkt;
Paul Moore56196702008-10-10 10:16:29 -0400764 struct list_head *iter_list;
Paul Moorefcd48282006-09-25 15:56:09 -0700765 struct netlbl_dom_map *iter_entry;
766 u32 chain_cnt = 0;
Paul Moored15c3452006-08-03 16:48:37 -0700767
Paul Moored15c3452006-08-03 16:48:37 -0700768 rcu_read_lock();
Paul Moorefcd48282006-09-25 15:56:09 -0700769 for (iter_bkt = *skip_bkt;
770 iter_bkt < rcu_dereference(netlbl_domhsh)->size;
771 iter_bkt++, chain_cnt = 0) {
Paul Moore56196702008-10-10 10:16:29 -0400772 iter_list = &rcu_dereference(netlbl_domhsh)->tbl[iter_bkt];
773 list_for_each_entry_rcu(iter_entry, iter_list, list)
Paul Moorefcd48282006-09-25 15:56:09 -0700774 if (iter_entry->valid) {
775 if (chain_cnt++ < *skip_chain)
776 continue;
777 ret_val = callback(iter_entry, cb_arg);
778 if (ret_val < 0) {
779 chain_cnt--;
780 goto walk_return;
781 }
Paul Moored15c3452006-08-03 16:48:37 -0700782 }
Paul Moorefcd48282006-09-25 15:56:09 -0700783 }
Paul Moored15c3452006-08-03 16:48:37 -0700784
Paul Moorefcd48282006-09-25 15:56:09 -0700785walk_return:
Paul Moored15c3452006-08-03 16:48:37 -0700786 rcu_read_unlock();
Paul Moorefcd48282006-09-25 15:56:09 -0700787 *skip_bkt = iter_bkt;
788 *skip_chain = chain_cnt;
789 return ret_val;
Paul Moored15c3452006-08-03 16:48:37 -0700790}