blob: f73a8382c275e0377c2c730c7ed6add3d4c3f93b [file] [log] [blame]
Thomas Gleixner1ccea772019-05-19 15:51:43 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Paul Moored15c3452006-08-03 16:48:37 -07002/*
3 * NetLabel Domain Hash Table
4 *
5 * This file manages the domain hash table that NetLabel uses to determine
6 * which network labeling protocol to use for a given domain. The NetLabel
7 * system manages static and dynamic label mappings for network protocols such
8 * as CIPSO and RIPSO.
9 *
Paul Moore82c21bf2011-08-01 11:10:33 +000010 * Author: Paul Moore <paul@paul-moore.com>
Paul Moored15c3452006-08-03 16:48:37 -070011 */
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
17#include <linux/types.h>
Franck Bui-Huu82524742008-05-12 21:21:05 +020018#include <linux/rculist.h>
Paul Moored15c3452006-08-03 16:48:37 -070019#include <linux/skbuff.h>
20#include <linux/spinlock.h>
21#include <linux/string.h>
Paul Moore32f50cd2006-09-28 14:51:47 -070022#include <linux/audit.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090023#include <linux/slab.h>
Paul Moored15c3452006-08-03 16:48:37 -070024#include <net/netlabel.h>
25#include <net/cipso_ipv4.h>
Huw Daviesdc7de732016-06-27 15:02:49 -040026#include <net/calipso.h>
Paul Moored15c3452006-08-03 16:48:37 -070027#include <asm/bug.h>
28
29#include "netlabel_mgmt.h"
Paul Moore63c41682008-10-10 10:16:32 -040030#include "netlabel_addrlist.h"
Huw Daviesdc7de732016-06-27 15:02:49 -040031#include "netlabel_calipso.h"
Paul Moored15c3452006-08-03 16:48:37 -070032#include "netlabel_domainhash.h"
Paul Moore32f50cd2006-09-28 14:51:47 -070033#include "netlabel_user.h"
Paul Moored15c3452006-08-03 16:48:37 -070034
35struct netlbl_domhsh_tbl {
36 struct list_head *tbl;
37 u32 size;
38};
39
40/* Domain hash table */
Paul Mooreb914f3a2010-04-01 10:43:57 +000041/* updates should be so rare that having one spinlock for the entire hash table
42 * should be okay */
Adrian Bunk8ce11e62006-08-07 21:50:48 -070043static DEFINE_SPINLOCK(netlbl_domhsh_lock);
Paul Mooreb914f3a2010-04-01 10:43:57 +000044#define netlbl_domhsh_rcu_deref(p) \
Michal Hockod8bf4ca2011-07-08 14:39:41 +020045 rcu_dereference_check(p, lockdep_is_held(&netlbl_domhsh_lock))
Huw Davies96a8f7f2016-06-27 15:02:45 -040046static struct netlbl_domhsh_tbl __rcu *netlbl_domhsh;
Huw Davies8f18e672016-06-27 15:02:46 -040047static struct netlbl_dom_map __rcu *netlbl_domhsh_def_ipv4;
48static struct netlbl_dom_map __rcu *netlbl_domhsh_def_ipv6;
Paul Moored15c3452006-08-03 16:48:37 -070049
50/*
51 * Domain Hash Table Helper Functions
52 */
53
54/**
55 * netlbl_domhsh_free_entry - Frees a domain hash table entry
56 * @entry: the entry's RCU field
57 *
58 * Description:
59 * This function is designed to be used as a callback to the call_rcu()
60 * function so that the memory allocated to a hash table entry can be released
61 * safely.
62 *
63 */
64static void netlbl_domhsh_free_entry(struct rcu_head *entry)
65{
66 struct netlbl_dom_map *ptr;
Paul Moore63c41682008-10-10 10:16:32 -040067 struct netlbl_af4list *iter4;
68 struct netlbl_af4list *tmp4;
Eric Dumazetdfd56b82011-12-10 09:48:31 +000069#if IS_ENABLED(CONFIG_IPV6)
Paul Moore63c41682008-10-10 10:16:32 -040070 struct netlbl_af6list *iter6;
71 struct netlbl_af6list *tmp6;
72#endif /* IPv6 */
Paul Moored15c3452006-08-03 16:48:37 -070073
74 ptr = container_of(entry, struct netlbl_dom_map, rcu);
Paul Moore6a8b7f02013-08-02 14:45:08 -040075 if (ptr->def.type == NETLBL_NLTYPE_ADDRSELECT) {
Paul Moore63c41682008-10-10 10:16:32 -040076 netlbl_af4list_foreach_safe(iter4, tmp4,
Paul Moore6a8b7f02013-08-02 14:45:08 -040077 &ptr->def.addrsel->list4) {
Paul Moore63c41682008-10-10 10:16:32 -040078 netlbl_af4list_remove_entry(iter4);
79 kfree(netlbl_domhsh_addr4_entry(iter4));
80 }
Eric Dumazetdfd56b82011-12-10 09:48:31 +000081#if IS_ENABLED(CONFIG_IPV6)
Paul Moore63c41682008-10-10 10:16:32 -040082 netlbl_af6list_foreach_safe(iter6, tmp6,
Paul Moore6a8b7f02013-08-02 14:45:08 -040083 &ptr->def.addrsel->list6) {
Paul Moore63c41682008-10-10 10:16:32 -040084 netlbl_af6list_remove_entry(iter6);
85 kfree(netlbl_domhsh_addr6_entry(iter6));
86 }
87#endif /* IPv6 */
Paul Moored3b990b2020-08-21 16:34:52 -040088 kfree(ptr->def.addrsel);
Paul Moore63c41682008-10-10 10:16:32 -040089 }
Paul Moored15c3452006-08-03 16:48:37 -070090 kfree(ptr->domain);
91 kfree(ptr);
92}
93
94/**
95 * netlbl_domhsh_hash - Hashing function for the domain hash table
Andrew Lunn26c3baa2020-07-13 01:15:07 +020096 * @key: the domain name to hash
Paul Moored15c3452006-08-03 16:48:37 -070097 *
98 * Description:
99 * This is the hashing function for the domain hash table, it returns the
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300100 * correct bucket number for the domain. The caller is responsible for
Paul Mooreb914f3a2010-04-01 10:43:57 +0000101 * ensuring that the hash table is protected with either a RCU read lock or the
102 * hash table lock.
Paul Moored15c3452006-08-03 16:48:37 -0700103 *
104 */
105static u32 netlbl_domhsh_hash(const char *key)
106{
107 u32 iter;
108 u32 val;
109 u32 len;
110
111 /* This is taken (with slight modification) from
112 * security/selinux/ss/symtab.c:symhash() */
113
114 for (iter = 0, val = 0, len = strlen(key); iter < len; iter++)
115 val = (val << 4 | (val >> (8 * sizeof(u32) - 4))) ^ key[iter];
Paul Mooreb914f3a2010-04-01 10:43:57 +0000116 return val & (netlbl_domhsh_rcu_deref(netlbl_domhsh)->size - 1);
Paul Moored15c3452006-08-03 16:48:37 -0700117}
118
Huw Davies8f18e672016-06-27 15:02:46 -0400119static bool netlbl_family_match(u16 f1, u16 f2)
120{
121 return (f1 == f2) || (f1 == AF_UNSPEC) || (f2 == AF_UNSPEC);
122}
123
Paul Moored15c3452006-08-03 16:48:37 -0700124/**
125 * netlbl_domhsh_search - Search for a domain entry
126 * @domain: the domain
Huw Davies8f18e672016-06-27 15:02:46 -0400127 * @family: the address family
Paul Moored15c3452006-08-03 16:48:37 -0700128 *
129 * Description:
130 * Searches the domain hash table and returns a pointer to the hash table
Huw Davies8f18e672016-06-27 15:02:46 -0400131 * entry if found, otherwise NULL is returned. @family may be %AF_UNSPEC
132 * which matches any address family entries. The caller is responsible for
Paul Mooreb914f3a2010-04-01 10:43:57 +0000133 * ensuring that the hash table is protected with either a RCU read lock or the
134 * hash table lock.
Paul Moored15c3452006-08-03 16:48:37 -0700135 *
136 */
Huw Davies8f18e672016-06-27 15:02:46 -0400137static struct netlbl_dom_map *netlbl_domhsh_search(const char *domain,
138 u16 family)
Paul Moored15c3452006-08-03 16:48:37 -0700139{
140 u32 bkt;
Paul Moore56196702008-10-10 10:16:29 -0400141 struct list_head *bkt_list;
Paul Moored15c3452006-08-03 16:48:37 -0700142 struct netlbl_dom_map *iter;
143
144 if (domain != NULL) {
145 bkt = netlbl_domhsh_hash(domain);
Paul Mooreb914f3a2010-04-01 10:43:57 +0000146 bkt_list = &netlbl_domhsh_rcu_deref(netlbl_domhsh)->tbl[bkt];
Madhuparna Bhowmik9facfdb2020-02-19 00:11:32 +0530147 list_for_each_entry_rcu(iter, bkt_list, list,
148 lockdep_is_held(&netlbl_domhsh_lock))
Huw Davies8f18e672016-06-27 15:02:46 -0400149 if (iter->valid &&
150 netlbl_family_match(iter->family, family) &&
151 strcmp(iter->domain, domain) == 0)
Paul Moored15c3452006-08-03 16:48:37 -0700152 return iter;
153 }
154
Paul Mooreb64397e2008-01-29 08:37:54 -0500155 return NULL;
156}
157
158/**
159 * netlbl_domhsh_search_def - Search for a domain entry
160 * @domain: the domain
Huw Davies8f18e672016-06-27 15:02:46 -0400161 * @family: the address family
Paul Mooreb64397e2008-01-29 08:37:54 -0500162 *
163 * Description:
164 * Searches the domain hash table and returns a pointer to the hash table
165 * entry if an exact match is found, if an exact match is not present in the
166 * hash table then the default entry is returned if valid otherwise NULL is
Huw Davies8f18e672016-06-27 15:02:46 -0400167 * returned. @family may be %AF_UNSPEC which matches any address family
168 * entries. The caller is responsible ensuring that the hash table is
Paul Mooreb914f3a2010-04-01 10:43:57 +0000169 * protected with either a RCU read lock or the hash table lock.
Paul Mooreb64397e2008-01-29 08:37:54 -0500170 *
171 */
Huw Davies8f18e672016-06-27 15:02:46 -0400172static struct netlbl_dom_map *netlbl_domhsh_search_def(const char *domain,
173 u16 family)
Paul Mooreb64397e2008-01-29 08:37:54 -0500174{
175 struct netlbl_dom_map *entry;
176
Huw Davies8f18e672016-06-27 15:02:46 -0400177 entry = netlbl_domhsh_search(domain, family);
178 if (entry != NULL)
179 return entry;
180 if (family == AF_INET || family == AF_UNSPEC) {
181 entry = netlbl_domhsh_rcu_deref(netlbl_domhsh_def_ipv4);
182 if (entry != NULL && entry->valid)
183 return entry;
184 }
185 if (family == AF_INET6 || family == AF_UNSPEC) {
186 entry = netlbl_domhsh_rcu_deref(netlbl_domhsh_def_ipv6);
187 if (entry != NULL && entry->valid)
188 return entry;
Paul Moored15c3452006-08-03 16:48:37 -0700189 }
190
Huw Davies8f18e672016-06-27 15:02:46 -0400191 return NULL;
Paul Moored15c3452006-08-03 16:48:37 -0700192}
193
Paul Moore63c41682008-10-10 10:16:32 -0400194/**
195 * netlbl_domhsh_audit_add - Generate an audit entry for an add event
196 * @entry: the entry being added
197 * @addr4: the IPv4 address information
198 * @addr6: the IPv6 address information
199 * @result: the result code
200 * @audit_info: NetLabel audit information
201 *
202 * Description:
203 * Generate an audit record for adding a new NetLabel/LSM mapping entry with
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300204 * the given information. Caller is responsible for holding the necessary
Paul Moore63c41682008-10-10 10:16:32 -0400205 * locks.
206 *
207 */
208static void netlbl_domhsh_audit_add(struct netlbl_dom_map *entry,
209 struct netlbl_af4list *addr4,
210 struct netlbl_af6list *addr6,
211 int result,
212 struct netlbl_audit *audit_info)
213{
214 struct audit_buffer *audit_buf;
215 struct cipso_v4_doi *cipsov4 = NULL;
Huw Daviesdc7de732016-06-27 15:02:49 -0400216 struct calipso_doi *calipso = NULL;
Paul Moore63c41682008-10-10 10:16:32 -0400217 u32 type;
218
219 audit_buf = netlbl_audit_start_common(AUDIT_MAC_MAP_ADD, audit_info);
220 if (audit_buf != NULL) {
221 audit_log_format(audit_buf, " nlbl_domain=%s",
222 entry->domain ? entry->domain : "(default)");
223 if (addr4 != NULL) {
224 struct netlbl_domaddr4_map *map4;
225 map4 = netlbl_domhsh_addr4_entry(addr4);
Paul Moore6a8b7f02013-08-02 14:45:08 -0400226 type = map4->def.type;
227 cipsov4 = map4->def.cipso;
Paul Moore63c41682008-10-10 10:16:32 -0400228 netlbl_af4list_audit_addr(audit_buf, 0, NULL,
229 addr4->addr, addr4->mask);
Eric Dumazetdfd56b82011-12-10 09:48:31 +0000230#if IS_ENABLED(CONFIG_IPV6)
Paul Moore63c41682008-10-10 10:16:32 -0400231 } else if (addr6 != NULL) {
232 struct netlbl_domaddr6_map *map6;
233 map6 = netlbl_domhsh_addr6_entry(addr6);
Paul Moore6a8b7f02013-08-02 14:45:08 -0400234 type = map6->def.type;
Huw Daviesdc7de732016-06-27 15:02:49 -0400235 calipso = map6->def.calipso;
Paul Moore63c41682008-10-10 10:16:32 -0400236 netlbl_af6list_audit_addr(audit_buf, 0, NULL,
237 &addr6->addr, &addr6->mask);
238#endif /* IPv6 */
239 } else {
Paul Moore6a8b7f02013-08-02 14:45:08 -0400240 type = entry->def.type;
241 cipsov4 = entry->def.cipso;
Huw Daviesdc7de732016-06-27 15:02:49 -0400242 calipso = entry->def.calipso;
Paul Moore63c41682008-10-10 10:16:32 -0400243 }
244 switch (type) {
245 case NETLBL_NLTYPE_UNLABELED:
246 audit_log_format(audit_buf, " nlbl_protocol=unlbl");
247 break;
248 case NETLBL_NLTYPE_CIPSOV4:
249 BUG_ON(cipsov4 == NULL);
250 audit_log_format(audit_buf,
251 " nlbl_protocol=cipsov4 cipso_doi=%u",
252 cipsov4->doi);
253 break;
Huw Daviesdc7de732016-06-27 15:02:49 -0400254 case NETLBL_NLTYPE_CALIPSO:
255 BUG_ON(calipso == NULL);
256 audit_log_format(audit_buf,
257 " nlbl_protocol=calipso calipso_doi=%u",
258 calipso->doi);
259 break;
Paul Moore63c41682008-10-10 10:16:32 -0400260 }
261 audit_log_format(audit_buf, " res=%u", result == 0 ? 1 : 0);
262 audit_log_end(audit_buf);
263 }
264}
265
Paul Moore6b21e1b2013-05-17 09:08:50 +0000266/**
267 * netlbl_domhsh_validate - Validate a new domain mapping entry
268 * @entry: the entry to validate
269 *
270 * This function validates the new domain mapping entry to ensure that it is
271 * a valid entry. Returns zero on success, negative values on failure.
272 *
273 */
274static int netlbl_domhsh_validate(const struct netlbl_dom_map *entry)
275{
276 struct netlbl_af4list *iter4;
277 struct netlbl_domaddr4_map *map4;
278#if IS_ENABLED(CONFIG_IPV6)
279 struct netlbl_af6list *iter6;
280 struct netlbl_domaddr6_map *map6;
281#endif /* IPv6 */
282
283 if (entry == NULL)
284 return -EINVAL;
285
Huw Davies8f18e672016-06-27 15:02:46 -0400286 if (entry->family != AF_INET && entry->family != AF_INET6 &&
287 (entry->family != AF_UNSPEC ||
288 entry->def.type != NETLBL_NLTYPE_UNLABELED))
289 return -EINVAL;
290
Paul Moore6a8b7f02013-08-02 14:45:08 -0400291 switch (entry->def.type) {
Paul Moore6b21e1b2013-05-17 09:08:50 +0000292 case NETLBL_NLTYPE_UNLABELED:
Huw Daviesdc7de732016-06-27 15:02:49 -0400293 if (entry->def.cipso != NULL || entry->def.calipso != NULL ||
294 entry->def.addrsel != NULL)
Paul Moore6b21e1b2013-05-17 09:08:50 +0000295 return -EINVAL;
296 break;
297 case NETLBL_NLTYPE_CIPSOV4:
Huw Davies8f18e672016-06-27 15:02:46 -0400298 if (entry->family != AF_INET ||
299 entry->def.cipso == NULL)
Paul Moore6b21e1b2013-05-17 09:08:50 +0000300 return -EINVAL;
301 break;
Huw Daviesdc7de732016-06-27 15:02:49 -0400302 case NETLBL_NLTYPE_CALIPSO:
303 if (entry->family != AF_INET6 ||
304 entry->def.calipso == NULL)
305 return -EINVAL;
306 break;
Paul Moore6b21e1b2013-05-17 09:08:50 +0000307 case NETLBL_NLTYPE_ADDRSELECT:
Paul Moore6a8b7f02013-08-02 14:45:08 -0400308 netlbl_af4list_foreach(iter4, &entry->def.addrsel->list4) {
Paul Moore6b21e1b2013-05-17 09:08:50 +0000309 map4 = netlbl_domhsh_addr4_entry(iter4);
Paul Moore6a8b7f02013-08-02 14:45:08 -0400310 switch (map4->def.type) {
Paul Moore6b21e1b2013-05-17 09:08:50 +0000311 case NETLBL_NLTYPE_UNLABELED:
Paul Moore6a8b7f02013-08-02 14:45:08 -0400312 if (map4->def.cipso != NULL)
Paul Moore6b21e1b2013-05-17 09:08:50 +0000313 return -EINVAL;
314 break;
315 case NETLBL_NLTYPE_CIPSOV4:
Paul Moore6a8b7f02013-08-02 14:45:08 -0400316 if (map4->def.cipso == NULL)
Paul Moore6b21e1b2013-05-17 09:08:50 +0000317 return -EINVAL;
318 break;
319 default:
320 return -EINVAL;
321 }
322 }
323#if IS_ENABLED(CONFIG_IPV6)
Paul Moore6a8b7f02013-08-02 14:45:08 -0400324 netlbl_af6list_foreach(iter6, &entry->def.addrsel->list6) {
Paul Moore6b21e1b2013-05-17 09:08:50 +0000325 map6 = netlbl_domhsh_addr6_entry(iter6);
Paul Moore6a8b7f02013-08-02 14:45:08 -0400326 switch (map6->def.type) {
Paul Moore6b21e1b2013-05-17 09:08:50 +0000327 case NETLBL_NLTYPE_UNLABELED:
Huw Daviesdc7de732016-06-27 15:02:49 -0400328 if (map6->def.calipso != NULL)
329 return -EINVAL;
330 break;
331 case NETLBL_NLTYPE_CALIPSO:
332 if (map6->def.calipso == NULL)
333 return -EINVAL;
Paul Moore6b21e1b2013-05-17 09:08:50 +0000334 break;
335 default:
336 return -EINVAL;
337 }
338 }
339#endif /* IPv6 */
340 break;
341 default:
342 return -EINVAL;
343 }
344
345 return 0;
346}
347
Paul Moored15c3452006-08-03 16:48:37 -0700348/*
349 * Domain Hash Table Functions
350 */
351
352/**
353 * netlbl_domhsh_init - Init for the domain hash
354 * @size: the number of bits to use for the hash buckets
355 *
356 * Description:
357 * Initializes the domain hash table, should be called only by
358 * netlbl_user_init() during initialization. Returns zero on success, non-zero
359 * values on error.
360 *
361 */
Pavel Emelyanov05705e42008-02-17 22:33:57 -0800362int __init netlbl_domhsh_init(u32 size)
Paul Moored15c3452006-08-03 16:48:37 -0700363{
364 u32 iter;
365 struct netlbl_domhsh_tbl *hsh_tbl;
366
367 if (size == 0)
368 return -EINVAL;
369
370 hsh_tbl = kmalloc(sizeof(*hsh_tbl), GFP_KERNEL);
371 if (hsh_tbl == NULL)
372 return -ENOMEM;
373 hsh_tbl->size = 1 << size;
374 hsh_tbl->tbl = kcalloc(hsh_tbl->size,
375 sizeof(struct list_head),
376 GFP_KERNEL);
377 if (hsh_tbl->tbl == NULL) {
378 kfree(hsh_tbl);
379 return -ENOMEM;
380 }
381 for (iter = 0; iter < hsh_tbl->size; iter++)
382 INIT_LIST_HEAD(&hsh_tbl->tbl[iter]);
383
Paul Moored15c3452006-08-03 16:48:37 -0700384 spin_lock(&netlbl_domhsh_lock);
Eric Dumazetcf778b02012-01-12 04:41:32 +0000385 rcu_assign_pointer(netlbl_domhsh, hsh_tbl);
Paul Moored15c3452006-08-03 16:48:37 -0700386 spin_unlock(&netlbl_domhsh_lock);
Paul Moored15c3452006-08-03 16:48:37 -0700387
388 return 0;
389}
390
391/**
392 * netlbl_domhsh_add - Adds a entry to the domain hash table
393 * @entry: the entry to add
Paul Moore95d4e6b2006-09-29 17:05:05 -0700394 * @audit_info: NetLabel audit information
Paul Moored15c3452006-08-03 16:48:37 -0700395 *
396 * Description:
397 * Adds a new entry to the domain hash table and handles any updates to the
Huw Davies8f18e672016-06-27 15:02:46 -0400398 * lower level protocol handler (i.e. CIPSO). @entry->family may be set to
399 * %AF_UNSPEC which will add an entry that matches all address families. This
400 * is only useful for the unlabelled type and will only succeed if there is no
401 * existing entry for any address family with the same domain. Returns zero
402 * on success, negative on failure.
Paul Moored15c3452006-08-03 16:48:37 -0700403 *
404 */
Paul Moore95d4e6b2006-09-29 17:05:05 -0700405int netlbl_domhsh_add(struct netlbl_dom_map *entry,
406 struct netlbl_audit *audit_info)
Paul Moored15c3452006-08-03 16:48:37 -0700407{
Paul Moore63c41682008-10-10 10:16:32 -0400408 int ret_val = 0;
Huw Davies8f18e672016-06-27 15:02:46 -0400409 struct netlbl_dom_map *entry_old, *entry_b;
Paul Moore63c41682008-10-10 10:16:32 -0400410 struct netlbl_af4list *iter4;
411 struct netlbl_af4list *tmp4;
Eric Dumazetdfd56b82011-12-10 09:48:31 +0000412#if IS_ENABLED(CONFIG_IPV6)
Paul Moore63c41682008-10-10 10:16:32 -0400413 struct netlbl_af6list *iter6;
414 struct netlbl_af6list *tmp6;
415#endif /* IPv6 */
Paul Moored15c3452006-08-03 16:48:37 -0700416
Paul Moore6b21e1b2013-05-17 09:08:50 +0000417 ret_val = netlbl_domhsh_validate(entry);
418 if (ret_val != 0)
419 return ret_val;
420
Paul Mooreb914f3a2010-04-01 10:43:57 +0000421 /* XXX - we can remove this RCU read lock as the spinlock protects the
422 * entire function, but before we do we need to fixup the
423 * netlbl_af[4,6]list RCU functions to do "the right thing" with
424 * respect to rcu_dereference() when only a spinlock is held. */
Paul Moored15c3452006-08-03 16:48:37 -0700425 rcu_read_lock();
Paul Moore1c3fad92008-01-29 08:37:57 -0500426 spin_lock(&netlbl_domhsh_lock);
Paul Moore63c41682008-10-10 10:16:32 -0400427 if (entry->domain != NULL)
Huw Davies8f18e672016-06-27 15:02:46 -0400428 entry_old = netlbl_domhsh_search(entry->domain, entry->family);
Paul Moore63c41682008-10-10 10:16:32 -0400429 else
Huw Davies8f18e672016-06-27 15:02:46 -0400430 entry_old = netlbl_domhsh_search_def(entry->domain,
431 entry->family);
Paul Moore63c41682008-10-10 10:16:32 -0400432 if (entry_old == NULL) {
433 entry->valid = 1;
Paul Moore63c41682008-10-10 10:16:32 -0400434
435 if (entry->domain != NULL) {
436 u32 bkt = netlbl_domhsh_hash(entry->domain);
Paul Moored15c3452006-08-03 16:48:37 -0700437 list_add_tail_rcu(&entry->list,
Paul Moore3482fd92007-08-07 17:53:10 -0700438 &rcu_dereference(netlbl_domhsh)->tbl[bkt]);
Paul Moore63c41682008-10-10 10:16:32 -0400439 } else {
440 INIT_LIST_HEAD(&entry->list);
Huw Davies8f18e672016-06-27 15:02:46 -0400441 switch (entry->family) {
442 case AF_INET:
443 rcu_assign_pointer(netlbl_domhsh_def_ipv4,
444 entry);
445 break;
446 case AF_INET6:
447 rcu_assign_pointer(netlbl_domhsh_def_ipv6,
448 entry);
449 break;
450 case AF_UNSPEC:
451 if (entry->def.type !=
452 NETLBL_NLTYPE_UNLABELED) {
453 ret_val = -EINVAL;
454 goto add_return;
455 }
456 entry_b = kzalloc(sizeof(*entry_b), GFP_ATOMIC);
457 if (entry_b == NULL) {
458 ret_val = -ENOMEM;
459 goto add_return;
460 }
461 entry_b->family = AF_INET6;
462 entry_b->def.type = NETLBL_NLTYPE_UNLABELED;
463 entry_b->valid = 1;
464 entry->family = AF_INET;
465 rcu_assign_pointer(netlbl_domhsh_def_ipv4,
466 entry);
467 rcu_assign_pointer(netlbl_domhsh_def_ipv6,
468 entry_b);
469 break;
470 default:
471 /* Already checked in
472 * netlbl_domhsh_validate(). */
473 ret_val = -EINVAL;
474 goto add_return;
475 }
Paul Moorede646882006-11-17 17:38:55 -0500476 }
Paul Moored15c3452006-08-03 16:48:37 -0700477
Paul Moore6a8b7f02013-08-02 14:45:08 -0400478 if (entry->def.type == NETLBL_NLTYPE_ADDRSELECT) {
Paul Moore63c41682008-10-10 10:16:32 -0400479 netlbl_af4list_foreach_rcu(iter4,
Paul Moore6a8b7f02013-08-02 14:45:08 -0400480 &entry->def.addrsel->list4)
Paul Moore63c41682008-10-10 10:16:32 -0400481 netlbl_domhsh_audit_add(entry, iter4, NULL,
482 ret_val, audit_info);
Eric Dumazetdfd56b82011-12-10 09:48:31 +0000483#if IS_ENABLED(CONFIG_IPV6)
Paul Moore63c41682008-10-10 10:16:32 -0400484 netlbl_af6list_foreach_rcu(iter6,
Paul Moore6a8b7f02013-08-02 14:45:08 -0400485 &entry->def.addrsel->list6)
Paul Moore63c41682008-10-10 10:16:32 -0400486 netlbl_domhsh_audit_add(entry, NULL, iter6,
487 ret_val, audit_info);
488#endif /* IPv6 */
489 } else
490 netlbl_domhsh_audit_add(entry, NULL, NULL,
491 ret_val, audit_info);
Paul Moore6a8b7f02013-08-02 14:45:08 -0400492 } else if (entry_old->def.type == NETLBL_NLTYPE_ADDRSELECT &&
493 entry->def.type == NETLBL_NLTYPE_ADDRSELECT) {
Paul Moore63c41682008-10-10 10:16:32 -0400494 struct list_head *old_list4;
495 struct list_head *old_list6;
496
Paul Moore6a8b7f02013-08-02 14:45:08 -0400497 old_list4 = &entry_old->def.addrsel->list4;
498 old_list6 = &entry_old->def.addrsel->list6;
Paul Moore63c41682008-10-10 10:16:32 -0400499
500 /* we only allow the addition of address selectors if all of
501 * the selectors do not exist in the existing domain map */
Paul Moore6a8b7f02013-08-02 14:45:08 -0400502 netlbl_af4list_foreach_rcu(iter4, &entry->def.addrsel->list4)
Paul Moore63c41682008-10-10 10:16:32 -0400503 if (netlbl_af4list_search_exact(iter4->addr,
504 iter4->mask,
505 old_list4)) {
506 ret_val = -EEXIST;
507 goto add_return;
508 }
Eric Dumazetdfd56b82011-12-10 09:48:31 +0000509#if IS_ENABLED(CONFIG_IPV6)
Paul Moore6a8b7f02013-08-02 14:45:08 -0400510 netlbl_af6list_foreach_rcu(iter6, &entry->def.addrsel->list6)
Paul Moore63c41682008-10-10 10:16:32 -0400511 if (netlbl_af6list_search_exact(&iter6->addr,
512 &iter6->mask,
513 old_list6)) {
514 ret_val = -EEXIST;
515 goto add_return;
516 }
517#endif /* IPv6 */
518
519 netlbl_af4list_foreach_safe(iter4, tmp4,
Paul Moore6a8b7f02013-08-02 14:45:08 -0400520 &entry->def.addrsel->list4) {
Paul Moore63c41682008-10-10 10:16:32 -0400521 netlbl_af4list_remove_entry(iter4);
522 iter4->valid = 1;
523 ret_val = netlbl_af4list_add(iter4, old_list4);
524 netlbl_domhsh_audit_add(entry_old, iter4, NULL,
525 ret_val, audit_info);
526 if (ret_val != 0)
527 goto add_return;
528 }
Eric Dumazetdfd56b82011-12-10 09:48:31 +0000529#if IS_ENABLED(CONFIG_IPV6)
Paul Moore63c41682008-10-10 10:16:32 -0400530 netlbl_af6list_foreach_safe(iter6, tmp6,
Paul Moore6a8b7f02013-08-02 14:45:08 -0400531 &entry->def.addrsel->list6) {
Paul Moore63c41682008-10-10 10:16:32 -0400532 netlbl_af6list_remove_entry(iter6);
533 iter6->valid = 1;
534 ret_val = netlbl_af6list_add(iter6, old_list6);
535 netlbl_domhsh_audit_add(entry_old, NULL, iter6,
536 ret_val, audit_info);
537 if (ret_val != 0)
538 goto add_return;
539 }
540#endif /* IPv6 */
Paul Moored3b990b2020-08-21 16:34:52 -0400541 /* cleanup the new entry since we've moved everything over */
542 netlbl_domhsh_free_entry(&entry->rcu);
Paul Moore63c41682008-10-10 10:16:32 -0400543 } else
544 ret_val = -EINVAL;
545
546add_return:
547 spin_unlock(&netlbl_domhsh_lock);
548 rcu_read_unlock();
Paul Moored15c3452006-08-03 16:48:37 -0700549 return ret_val;
550}
551
552/**
553 * netlbl_domhsh_add_default - Adds the default entry to the domain hash table
554 * @entry: the entry to add
Paul Moore95d4e6b2006-09-29 17:05:05 -0700555 * @audit_info: NetLabel audit information
Paul Moored15c3452006-08-03 16:48:37 -0700556 *
557 * Description:
558 * Adds a new default entry to the domain hash table and handles any updates
559 * to the lower level protocol handler (i.e. CIPSO). Returns zero on success,
560 * negative on failure.
561 *
562 */
Paul Moore95d4e6b2006-09-29 17:05:05 -0700563int netlbl_domhsh_add_default(struct netlbl_dom_map *entry,
564 struct netlbl_audit *audit_info)
Paul Moored15c3452006-08-03 16:48:37 -0700565{
Paul Moore95d4e6b2006-09-29 17:05:05 -0700566 return netlbl_domhsh_add(entry, audit_info);
Paul Moored15c3452006-08-03 16:48:37 -0700567}
568
569/**
Paul Mooreb1edeb12008-10-10 10:16:31 -0400570 * netlbl_domhsh_remove_entry - Removes a given entry from the domain table
571 * @entry: the entry to remove
572 * @audit_info: NetLabel audit information
573 *
574 * Description:
575 * Removes an entry from the domain hash table and handles any updates to the
576 * lower level protocol handler (i.e. CIPSO). Caller is responsible for
577 * ensuring that the RCU read lock is held. Returns zero on success, negative
578 * on failure.
579 *
580 */
581int netlbl_domhsh_remove_entry(struct netlbl_dom_map *entry,
582 struct netlbl_audit *audit_info)
583{
584 int ret_val = 0;
585 struct audit_buffer *audit_buf;
Paul Moored3b990b2020-08-21 16:34:52 -0400586 struct netlbl_af4list *iter4;
587 struct netlbl_domaddr4_map *map4;
588#if IS_ENABLED(CONFIG_IPV6)
589 struct netlbl_af6list *iter6;
590 struct netlbl_domaddr6_map *map6;
591#endif /* IPv6 */
Paul Mooreb1edeb12008-10-10 10:16:31 -0400592
593 if (entry == NULL)
594 return -ENOENT;
595
596 spin_lock(&netlbl_domhsh_lock);
597 if (entry->valid) {
598 entry->valid = 0;
Huw Davies8f18e672016-06-27 15:02:46 -0400599 if (entry == rcu_dereference(netlbl_domhsh_def_ipv4))
600 RCU_INIT_POINTER(netlbl_domhsh_def_ipv4, NULL);
601 else if (entry == rcu_dereference(netlbl_domhsh_def_ipv6))
602 RCU_INIT_POINTER(netlbl_domhsh_def_ipv6, NULL);
Paul Mooreb1edeb12008-10-10 10:16:31 -0400603 else
Huw Davies8f18e672016-06-27 15:02:46 -0400604 list_del_rcu(&entry->list);
Paul Mooreb1edeb12008-10-10 10:16:31 -0400605 } else
606 ret_val = -ENOENT;
607 spin_unlock(&netlbl_domhsh_lock);
608
Paul Moored3b990b2020-08-21 16:34:52 -0400609 if (ret_val)
610 return ret_val;
611
Paul Mooreb1edeb12008-10-10 10:16:31 -0400612 audit_buf = netlbl_audit_start_common(AUDIT_MAC_MAP_DEL, audit_info);
613 if (audit_buf != NULL) {
614 audit_log_format(audit_buf,
615 " nlbl_domain=%s res=%u",
616 entry->domain ? entry->domain : "(default)",
617 ret_val == 0 ? 1 : 0);
618 audit_log_end(audit_buf);
619 }
620
Paul Moored3b990b2020-08-21 16:34:52 -0400621 switch (entry->def.type) {
622 case NETLBL_NLTYPE_ADDRSELECT:
623 netlbl_af4list_foreach_rcu(iter4, &entry->def.addrsel->list4) {
624 map4 = netlbl_domhsh_addr4_entry(iter4);
625 cipso_v4_doi_putdef(map4->def.cipso);
Paul Mooreb1edeb12008-10-10 10:16:31 -0400626 }
Paul Moored3b990b2020-08-21 16:34:52 -0400627#if IS_ENABLED(CONFIG_IPV6)
628 netlbl_af6list_foreach_rcu(iter6, &entry->def.addrsel->list6) {
629 map6 = netlbl_domhsh_addr6_entry(iter6);
630 calipso_doi_putdef(map6->def.calipso);
631 }
632#endif /* IPv6 */
633 break;
634 case NETLBL_NLTYPE_CIPSOV4:
635 cipso_v4_doi_putdef(entry->def.cipso);
636 break;
637#if IS_ENABLED(CONFIG_IPV6)
638 case NETLBL_NLTYPE_CALIPSO:
639 calipso_doi_putdef(entry->def.calipso);
640 break;
641#endif /* IPv6 */
Paul Mooreb1edeb12008-10-10 10:16:31 -0400642 }
Paul Moored3b990b2020-08-21 16:34:52 -0400643 call_rcu(&entry->rcu, netlbl_domhsh_free_entry);
Paul Mooreb1edeb12008-10-10 10:16:31 -0400644
645 return ret_val;
646}
647
648/**
Paul Moore6c2e8ac2008-12-31 12:54:11 -0500649 * netlbl_domhsh_remove_af4 - Removes an address selector entry
650 * @domain: the domain
651 * @addr: IPv4 address
652 * @mask: IPv4 address mask
653 * @audit_info: NetLabel audit information
654 *
655 * Description:
656 * Removes an individual address selector from a domain mapping and potentially
657 * the entire mapping if it is empty. Returns zero on success, negative values
658 * on failure.
659 *
660 */
661int netlbl_domhsh_remove_af4(const char *domain,
662 const struct in_addr *addr,
663 const struct in_addr *mask,
664 struct netlbl_audit *audit_info)
665{
666 struct netlbl_dom_map *entry_map;
667 struct netlbl_af4list *entry_addr;
668 struct netlbl_af4list *iter4;
Eric Dumazetdfd56b82011-12-10 09:48:31 +0000669#if IS_ENABLED(CONFIG_IPV6)
Paul Moore6c2e8ac2008-12-31 12:54:11 -0500670 struct netlbl_af6list *iter6;
671#endif /* IPv6 */
672 struct netlbl_domaddr4_map *entry;
673
674 rcu_read_lock();
675
676 if (domain)
Huw Davies8f18e672016-06-27 15:02:46 -0400677 entry_map = netlbl_domhsh_search(domain, AF_INET);
Paul Moore6c2e8ac2008-12-31 12:54:11 -0500678 else
Huw Davies8f18e672016-06-27 15:02:46 -0400679 entry_map = netlbl_domhsh_search_def(domain, AF_INET);
Paul Moore6a8b7f02013-08-02 14:45:08 -0400680 if (entry_map == NULL ||
681 entry_map->def.type != NETLBL_NLTYPE_ADDRSELECT)
Paul Moore6c2e8ac2008-12-31 12:54:11 -0500682 goto remove_af4_failure;
683
684 spin_lock(&netlbl_domhsh_lock);
685 entry_addr = netlbl_af4list_remove(addr->s_addr, mask->s_addr,
Paul Moore6a8b7f02013-08-02 14:45:08 -0400686 &entry_map->def.addrsel->list4);
Paul Moore6c2e8ac2008-12-31 12:54:11 -0500687 spin_unlock(&netlbl_domhsh_lock);
688
689 if (entry_addr == NULL)
690 goto remove_af4_failure;
Paul Moore6a8b7f02013-08-02 14:45:08 -0400691 netlbl_af4list_foreach_rcu(iter4, &entry_map->def.addrsel->list4)
Paul Moore6c2e8ac2008-12-31 12:54:11 -0500692 goto remove_af4_single_addr;
Eric Dumazetdfd56b82011-12-10 09:48:31 +0000693#if IS_ENABLED(CONFIG_IPV6)
Paul Moore6a8b7f02013-08-02 14:45:08 -0400694 netlbl_af6list_foreach_rcu(iter6, &entry_map->def.addrsel->list6)
Paul Moore6c2e8ac2008-12-31 12:54:11 -0500695 goto remove_af4_single_addr;
696#endif /* IPv6 */
697 /* the domain mapping is empty so remove it from the mapping table */
698 netlbl_domhsh_remove_entry(entry_map, audit_info);
699
700remove_af4_single_addr:
701 rcu_read_unlock();
702 /* yick, we can't use call_rcu here because we don't have a rcu head
703 * pointer but hopefully this should be a rare case so the pause
704 * shouldn't be a problem */
705 synchronize_rcu();
706 entry = netlbl_domhsh_addr4_entry(entry_addr);
Paul Moore6a8b7f02013-08-02 14:45:08 -0400707 cipso_v4_doi_putdef(entry->def.cipso);
Paul Moore6c2e8ac2008-12-31 12:54:11 -0500708 kfree(entry);
709 return 0;
710
711remove_af4_failure:
712 rcu_read_unlock();
713 return -ENOENT;
714}
715
Huw Davies3f093542016-06-27 15:06:18 -0400716#if IS_ENABLED(CONFIG_IPV6)
717/**
718 * netlbl_domhsh_remove_af6 - Removes an address selector entry
719 * @domain: the domain
720 * @addr: IPv6 address
721 * @mask: IPv6 address mask
722 * @audit_info: NetLabel audit information
723 *
724 * Description:
725 * Removes an individual address selector from a domain mapping and potentially
726 * the entire mapping if it is empty. Returns zero on success, negative values
727 * on failure.
728 *
729 */
730int netlbl_domhsh_remove_af6(const char *domain,
731 const struct in6_addr *addr,
732 const struct in6_addr *mask,
733 struct netlbl_audit *audit_info)
734{
735 struct netlbl_dom_map *entry_map;
736 struct netlbl_af6list *entry_addr;
737 struct netlbl_af4list *iter4;
738 struct netlbl_af6list *iter6;
739 struct netlbl_domaddr6_map *entry;
740
741 rcu_read_lock();
742
743 if (domain)
744 entry_map = netlbl_domhsh_search(domain, AF_INET6);
745 else
746 entry_map = netlbl_domhsh_search_def(domain, AF_INET6);
747 if (entry_map == NULL ||
748 entry_map->def.type != NETLBL_NLTYPE_ADDRSELECT)
749 goto remove_af6_failure;
750
751 spin_lock(&netlbl_domhsh_lock);
752 entry_addr = netlbl_af6list_remove(addr, mask,
753 &entry_map->def.addrsel->list6);
754 spin_unlock(&netlbl_domhsh_lock);
755
756 if (entry_addr == NULL)
757 goto remove_af6_failure;
758 netlbl_af4list_foreach_rcu(iter4, &entry_map->def.addrsel->list4)
759 goto remove_af6_single_addr;
760 netlbl_af6list_foreach_rcu(iter6, &entry_map->def.addrsel->list6)
761 goto remove_af6_single_addr;
762 /* the domain mapping is empty so remove it from the mapping table */
763 netlbl_domhsh_remove_entry(entry_map, audit_info);
764
765remove_af6_single_addr:
766 rcu_read_unlock();
767 /* yick, we can't use call_rcu here because we don't have a rcu head
768 * pointer but hopefully this should be a rare case so the pause
769 * shouldn't be a problem */
770 synchronize_rcu();
771 entry = netlbl_domhsh_addr6_entry(entry_addr);
772 calipso_doi_putdef(entry->def.calipso);
773 kfree(entry);
774 return 0;
775
776remove_af6_failure:
777 rcu_read_unlock();
778 return -ENOENT;
779}
780#endif /* IPv6 */
781
Paul Moore6c2e8ac2008-12-31 12:54:11 -0500782/**
Paul Moored15c3452006-08-03 16:48:37 -0700783 * netlbl_domhsh_remove - Removes an entry from the domain hash table
784 * @domain: the domain to remove
Huw Davies8f18e672016-06-27 15:02:46 -0400785 * @family: address family
Paul Moore95d4e6b2006-09-29 17:05:05 -0700786 * @audit_info: NetLabel audit information
Paul Moored15c3452006-08-03 16:48:37 -0700787 *
788 * Description:
789 * Removes an entry from the domain hash table and handles any updates to the
Huw Davies8f18e672016-06-27 15:02:46 -0400790 * lower level protocol handler (i.e. CIPSO). @family may be %AF_UNSPEC which
791 * removes all address family entries. Returns zero on success, negative on
792 * failure.
Paul Moored15c3452006-08-03 16:48:37 -0700793 *
794 */
Huw Davies8f18e672016-06-27 15:02:46 -0400795int netlbl_domhsh_remove(const char *domain, u16 family,
796 struct netlbl_audit *audit_info)
Paul Moored15c3452006-08-03 16:48:37 -0700797{
Huw Davies8f18e672016-06-27 15:02:46 -0400798 int ret_val = -EINVAL;
Paul Moored15c3452006-08-03 16:48:37 -0700799 struct netlbl_dom_map *entry;
800
801 rcu_read_lock();
Huw Davies8f18e672016-06-27 15:02:46 -0400802
803 if (family == AF_INET || family == AF_UNSPEC) {
804 if (domain)
805 entry = netlbl_domhsh_search(domain, AF_INET);
806 else
807 entry = netlbl_domhsh_search_def(domain, AF_INET);
808 ret_val = netlbl_domhsh_remove_entry(entry, audit_info);
809 if (ret_val && ret_val != -ENOENT)
810 goto done;
811 }
812 if (family == AF_INET6 || family == AF_UNSPEC) {
813 int ret_val2;
814
815 if (domain)
816 entry = netlbl_domhsh_search(domain, AF_INET6);
817 else
818 entry = netlbl_domhsh_search_def(domain, AF_INET6);
819 ret_val2 = netlbl_domhsh_remove_entry(entry, audit_info);
820 if (ret_val2 != -ENOENT)
821 ret_val = ret_val2;
822 }
823done:
Paul Moored15c3452006-08-03 16:48:37 -0700824 rcu_read_unlock();
Paul Mooreb1edeb12008-10-10 10:16:31 -0400825
Paul Moored15c3452006-08-03 16:48:37 -0700826 return ret_val;
827}
828
829/**
830 * netlbl_domhsh_remove_default - Removes the default entry from the table
Huw Davies8f18e672016-06-27 15:02:46 -0400831 * @family: address family
Paul Moore95d4e6b2006-09-29 17:05:05 -0700832 * @audit_info: NetLabel audit information
Paul Moored15c3452006-08-03 16:48:37 -0700833 *
834 * Description:
Huw Davies8f18e672016-06-27 15:02:46 -0400835 * Removes/resets the default entry corresponding to @family from the domain
836 * hash table and handles any updates to the lower level protocol handler
837 * (i.e. CIPSO). @family may be %AF_UNSPEC which removes all address family
838 * entries. Returns zero on success, negative on failure.
Paul Moored15c3452006-08-03 16:48:37 -0700839 *
840 */
Huw Davies8f18e672016-06-27 15:02:46 -0400841int netlbl_domhsh_remove_default(u16 family, struct netlbl_audit *audit_info)
Paul Moored15c3452006-08-03 16:48:37 -0700842{
Huw Davies8f18e672016-06-27 15:02:46 -0400843 return netlbl_domhsh_remove(NULL, family, audit_info);
Paul Moored15c3452006-08-03 16:48:37 -0700844}
845
846/**
847 * netlbl_domhsh_getentry - Get an entry from the domain hash table
848 * @domain: the domain name to search for
Huw Davies8f18e672016-06-27 15:02:46 -0400849 * @family: address family
Paul Moored15c3452006-08-03 16:48:37 -0700850 *
851 * Description:
852 * Look through the domain hash table searching for an entry to match @domain,
Huw Davies8f18e672016-06-27 15:02:46 -0400853 * with address family @family, return a pointer to a copy of the entry or
854 * NULL. The caller is responsible for ensuring that rcu_read_[un]lock() is
855 * called.
Paul Moored15c3452006-08-03 16:48:37 -0700856 *
857 */
Huw Davies8f18e672016-06-27 15:02:46 -0400858struct netlbl_dom_map *netlbl_domhsh_getentry(const char *domain, u16 family)
Paul Moored15c3452006-08-03 16:48:37 -0700859{
Huw Davies8f18e672016-06-27 15:02:46 -0400860 if (family == AF_UNSPEC)
861 return NULL;
862 return netlbl_domhsh_search_def(domain, family);
Paul Moored15c3452006-08-03 16:48:37 -0700863}
864
865/**
Paul Moore63c41682008-10-10 10:16:32 -0400866 * netlbl_domhsh_getentry_af4 - Get an entry from the domain hash table
867 * @domain: the domain name to search for
868 * @addr: the IP address to search for
869 *
870 * Description:
871 * Look through the domain hash table searching for an entry to match @domain
872 * and @addr, return a pointer to a copy of the entry or NULL. The caller is
873 * responsible for ensuring that rcu_read_[un]lock() is called.
874 *
875 */
Paul Moore6a8b7f02013-08-02 14:45:08 -0400876struct netlbl_dommap_def *netlbl_domhsh_getentry_af4(const char *domain,
877 __be32 addr)
Paul Moore63c41682008-10-10 10:16:32 -0400878{
879 struct netlbl_dom_map *dom_iter;
880 struct netlbl_af4list *addr_iter;
881
Huw Davies8f18e672016-06-27 15:02:46 -0400882 dom_iter = netlbl_domhsh_search_def(domain, AF_INET);
Paul Moore63c41682008-10-10 10:16:32 -0400883 if (dom_iter == NULL)
884 return NULL;
Paul Moore63c41682008-10-10 10:16:32 -0400885
Paul Moore6a8b7f02013-08-02 14:45:08 -0400886 if (dom_iter->def.type != NETLBL_NLTYPE_ADDRSELECT)
887 return &dom_iter->def;
888 addr_iter = netlbl_af4list_search(addr, &dom_iter->def.addrsel->list4);
Paul Moore63c41682008-10-10 10:16:32 -0400889 if (addr_iter == NULL)
890 return NULL;
Paul Moore6a8b7f02013-08-02 14:45:08 -0400891 return &(netlbl_domhsh_addr4_entry(addr_iter)->def);
Paul Moore63c41682008-10-10 10:16:32 -0400892}
893
Eric Dumazetdfd56b82011-12-10 09:48:31 +0000894#if IS_ENABLED(CONFIG_IPV6)
Paul Moore63c41682008-10-10 10:16:32 -0400895/**
896 * netlbl_domhsh_getentry_af6 - Get an entry from the domain hash table
897 * @domain: the domain name to search for
898 * @addr: the IP address to search for
899 *
900 * Description:
901 * Look through the domain hash table searching for an entry to match @domain
902 * and @addr, return a pointer to a copy of the entry or NULL. The caller is
903 * responsible for ensuring that rcu_read_[un]lock() is called.
904 *
905 */
Paul Moore6a8b7f02013-08-02 14:45:08 -0400906struct netlbl_dommap_def *netlbl_domhsh_getentry_af6(const char *domain,
Paul Moore63c41682008-10-10 10:16:32 -0400907 const struct in6_addr *addr)
908{
909 struct netlbl_dom_map *dom_iter;
910 struct netlbl_af6list *addr_iter;
911
Huw Davies8f18e672016-06-27 15:02:46 -0400912 dom_iter = netlbl_domhsh_search_def(domain, AF_INET6);
Paul Moore63c41682008-10-10 10:16:32 -0400913 if (dom_iter == NULL)
914 return NULL;
Paul Moore63c41682008-10-10 10:16:32 -0400915
Paul Moore6a8b7f02013-08-02 14:45:08 -0400916 if (dom_iter->def.type != NETLBL_NLTYPE_ADDRSELECT)
917 return &dom_iter->def;
918 addr_iter = netlbl_af6list_search(addr, &dom_iter->def.addrsel->list6);
Paul Moore63c41682008-10-10 10:16:32 -0400919 if (addr_iter == NULL)
920 return NULL;
Paul Moore6a8b7f02013-08-02 14:45:08 -0400921 return &(netlbl_domhsh_addr6_entry(addr_iter)->def);
Paul Moore63c41682008-10-10 10:16:32 -0400922}
923#endif /* IPv6 */
924
925/**
Paul Moorefcd48282006-09-25 15:56:09 -0700926 * netlbl_domhsh_walk - Iterate through the domain mapping hash table
927 * @skip_bkt: the number of buckets to skip at the start
928 * @skip_chain: the number of entries to skip in the first iterated bucket
929 * @callback: callback for each entry
930 * @cb_arg: argument for the callback function
Paul Moored15c3452006-08-03 16:48:37 -0700931 *
932 * Description:
Paul Moorefcd48282006-09-25 15:56:09 -0700933 * Interate over the domain mapping hash table, skipping the first @skip_bkt
934 * buckets and @skip_chain entries. For each entry in the table call
935 * @callback, if @callback returns a negative value stop 'walking' through the
936 * table and return. Updates the values in @skip_bkt and @skip_chain on
André Goddard Rosaaf901ca2009-11-14 13:09:05 -0200937 * return. Returns zero on success, negative values on failure.
Paul Moored15c3452006-08-03 16:48:37 -0700938 *
939 */
Paul Moorefcd48282006-09-25 15:56:09 -0700940int netlbl_domhsh_walk(u32 *skip_bkt,
941 u32 *skip_chain,
942 int (*callback) (struct netlbl_dom_map *entry, void *arg),
943 void *cb_arg)
Paul Moored15c3452006-08-03 16:48:37 -0700944{
Paul Moorefcd48282006-09-25 15:56:09 -0700945 int ret_val = -ENOENT;
946 u32 iter_bkt;
Paul Moore56196702008-10-10 10:16:29 -0400947 struct list_head *iter_list;
Paul Moorefcd48282006-09-25 15:56:09 -0700948 struct netlbl_dom_map *iter_entry;
949 u32 chain_cnt = 0;
Paul Moored15c3452006-08-03 16:48:37 -0700950
Paul Moored15c3452006-08-03 16:48:37 -0700951 rcu_read_lock();
Paul Moorefcd48282006-09-25 15:56:09 -0700952 for (iter_bkt = *skip_bkt;
953 iter_bkt < rcu_dereference(netlbl_domhsh)->size;
954 iter_bkt++, chain_cnt = 0) {
Paul Moore56196702008-10-10 10:16:29 -0400955 iter_list = &rcu_dereference(netlbl_domhsh)->tbl[iter_bkt];
956 list_for_each_entry_rcu(iter_entry, iter_list, list)
Paul Moorefcd48282006-09-25 15:56:09 -0700957 if (iter_entry->valid) {
958 if (chain_cnt++ < *skip_chain)
959 continue;
960 ret_val = callback(iter_entry, cb_arg);
961 if (ret_val < 0) {
962 chain_cnt--;
963 goto walk_return;
964 }
Paul Moored15c3452006-08-03 16:48:37 -0700965 }
Paul Moorefcd48282006-09-25 15:56:09 -0700966 }
Paul Moored15c3452006-08-03 16:48:37 -0700967
Paul Moorefcd48282006-09-25 15:56:09 -0700968walk_return:
Paul Moored15c3452006-08-03 16:48:37 -0700969 rcu_read_unlock();
Paul Moorefcd48282006-09-25 15:56:09 -0700970 *skip_bkt = iter_bkt;
971 *skip_chain = chain_cnt;
972 return ret_val;
Paul Moored15c3452006-08-03 16:48:37 -0700973}