2 * NetLabel Network Address Lists
4 * This file contains network address list functions used to manage ordered
5 * lists of network addresses for use by the NetLabel subsystem. The NetLabel
6 * system manages static and dynamic label mappings for network protocols such
9 * Author: Paul Moore <paul.moore@hp.com>
14 * (c) Copyright Hewlett-Packard Development Company, L.P., 2008
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.
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.
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
28 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
32 #include <linux/types.h>
33 #include <linux/rcupdate.h>
34 #include <linux/list.h>
35 #include <linux/spinlock.h>
37 #include <linux/in6.h>
39 #include <linux/ipv6.h>
42 #include <linux/audit.h>
44 #include "netlabel_addrlist.h"
47 * Address List Functions
51 * netlbl_af4list_search - Search for a matching IPv4 address entry
53 * @head: the list head
56 * Searches the IPv4 address list given by @head. If a matching address entry
57 * is found it is returned, otherwise NULL is returned. The caller is
58 * responsible for calling the rcu_read_[un]lock() functions.
61 struct netlbl_af4list
*netlbl_af4list_search(__be32 addr
,
62 struct list_head
*head
)
64 struct netlbl_af4list
*iter
;
66 list_for_each_entry_rcu(iter
, head
, list
)
67 if (iter
->valid
&& (addr
& iter
->mask
) == iter
->addr
)
74 * netlbl_af4list_search_exact - Search for an exact IPv4 address entry
76 * @mask: IPv4 address mask
77 * @head: the list head
80 * Searches the IPv4 address list given by @head. If an exact match if found
81 * it is returned, otherwise NULL is returned. The caller is responsible for
82 * calling the rcu_read_[un]lock() functions.
85 struct netlbl_af4list
*netlbl_af4list_search_exact(__be32 addr
,
87 struct list_head
*head
)
89 struct netlbl_af4list
*iter
;
91 list_for_each_entry_rcu(iter
, head
, list
)
92 if (iter
->valid
&& iter
->addr
== addr
&& iter
->mask
== mask
)
99 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
101 * netlbl_af6list_search - Search for a matching IPv6 address entry
102 * @addr: IPv6 address
103 * @head: the list head
106 * Searches the IPv6 address list given by @head. If a matching address entry
107 * is found it is returned, otherwise NULL is returned. The caller is
108 * responsible for calling the rcu_read_[un]lock() functions.
111 struct netlbl_af6list
*netlbl_af6list_search(const struct in6_addr
*addr
,
112 struct list_head
*head
)
114 struct netlbl_af6list
*iter
;
116 list_for_each_entry_rcu(iter
, head
, list
)
118 ipv6_masked_addr_cmp(&iter
->addr
, &iter
->mask
, addr
) == 0)
125 * netlbl_af6list_search_exact - Search for an exact IPv6 address entry
126 * @addr: IPv6 address
127 * @mask: IPv6 address mask
128 * @head: the list head
131 * Searches the IPv6 address list given by @head. If an exact match if found
132 * it is returned, otherwise NULL is returned. The caller is responsible for
133 * calling the rcu_read_[un]lock() functions.
136 struct netlbl_af6list
*netlbl_af6list_search_exact(const struct in6_addr
*addr
,
137 const struct in6_addr
*mask
,
138 struct list_head
*head
)
140 struct netlbl_af6list
*iter
;
142 list_for_each_entry_rcu(iter
, head
, list
)
144 ipv6_addr_equal(&iter
->addr
, addr
) &&
145 ipv6_addr_equal(&iter
->mask
, mask
))
153 * netlbl_af4list_add - Add a new IPv4 address entry to a list
154 * @entry: address entry
155 * @head: the list head
158 * Add a new address entry to the list pointed to by @head. On success zero is
159 * returned, otherwise a negative value is returned. The caller is responsible
160 * for calling the necessary locking functions.
163 int netlbl_af4list_add(struct netlbl_af4list
*entry
, struct list_head
*head
)
165 struct netlbl_af4list
*iter
;
167 iter
= netlbl_af4list_search(entry
->addr
, head
);
169 iter
->addr
== entry
->addr
&& iter
->mask
== entry
->mask
)
172 /* in order to speed up address searches through the list (the common
173 * case) we need to keep the list in order based on the size of the
174 * address mask such that the entry with the widest mask (smallest
175 * numerical value) appears first in the list */
176 list_for_each_entry_rcu(iter
, head
, list
)
178 ntohl(entry
->mask
) > ntohl(iter
->mask
)) {
179 __list_add_rcu(&entry
->list
,
184 list_add_tail_rcu(&entry
->list
, head
);
188 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
190 * netlbl_af6list_add - Add a new IPv6 address entry to a list
191 * @entry: address entry
192 * @head: the list head
195 * Add a new address entry to the list pointed to by @head. On success zero is
196 * returned, otherwise a negative value is returned. The caller is responsible
197 * for calling the necessary locking functions.
200 int netlbl_af6list_add(struct netlbl_af6list
*entry
, struct list_head
*head
)
202 struct netlbl_af6list
*iter
;
204 iter
= netlbl_af6list_search(&entry
->addr
, head
);
206 ipv6_addr_equal(&iter
->addr
, &entry
->addr
) &&
207 ipv6_addr_equal(&iter
->mask
, &entry
->mask
))
210 /* in order to speed up address searches through the list (the common
211 * case) we need to keep the list in order based on the size of the
212 * address mask such that the entry with the widest mask (smallest
213 * numerical value) appears first in the list */
214 list_for_each_entry_rcu(iter
, head
, list
)
216 ipv6_addr_cmp(&entry
->mask
, &iter
->mask
) > 0) {
217 __list_add_rcu(&entry
->list
,
222 list_add_tail_rcu(&entry
->list
, head
);
228 * netlbl_af4list_remove_entry - Remove an IPv4 address entry
229 * @entry: address entry
232 * Remove the specified IP address entry. The caller is responsible for
233 * calling the necessary locking functions.
236 void netlbl_af4list_remove_entry(struct netlbl_af4list
*entry
)
239 list_del_rcu(&entry
->list
);
243 * netlbl_af4list_remove - Remove an IPv4 address entry
245 * @mask: IP address mask
246 * @head: the list head
249 * Remove an IP address entry from the list pointed to by @head. Returns the
250 * entry on success, NULL on failure. The caller is responsible for calling
251 * the necessary locking functions.
254 struct netlbl_af4list
*netlbl_af4list_remove(__be32 addr
, __be32 mask
,
255 struct list_head
*head
)
257 struct netlbl_af4list
*entry
;
259 entry
= netlbl_af4list_search_exact(addr
, mask
, head
);
262 netlbl_af4list_remove_entry(entry
);
266 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
268 * netlbl_af6list_remove_entry - Remove an IPv6 address entry
269 * @entry: address entry
272 * Remove the specified IP address entry. The caller is responsible for
273 * calling the necessary locking functions.
276 void netlbl_af6list_remove_entry(struct netlbl_af6list
*entry
)
279 list_del_rcu(&entry
->list
);
283 * netlbl_af6list_remove - Remove an IPv6 address entry
285 * @mask: IP address mask
286 * @head: the list head
289 * Remove an IP address entry from the list pointed to by @head. Returns the
290 * entry on success, NULL on failure. The caller is responsible for calling
291 * the necessary locking functions.
294 struct netlbl_af6list
*netlbl_af6list_remove(const struct in6_addr
*addr
,
295 const struct in6_addr
*mask
,
296 struct list_head
*head
)
298 struct netlbl_af6list
*entry
;
300 entry
= netlbl_af6list_search_exact(addr
, mask
, head
);
303 netlbl_af6list_remove_entry(entry
);
309 * Audit Helper Functions
314 * netlbl_af4list_audit_addr - Audit an IPv4 address
315 * @audit_buf: audit buffer
316 * @src: true if source address, false if destination
317 * @dev: network interface
319 * @mask: IP address mask
322 * Write the IPv4 address and address mask, if necessary, to @audit_buf.
325 void netlbl_af4list_audit_addr(struct audit_buffer
*audit_buf
,
326 int src
, const char *dev
,
327 __be32 addr
, __be32 mask
)
329 u32 mask_val
= ntohl(mask
);
330 char *dir
= (src
? "src" : "dst");
333 audit_log_format(audit_buf
, " netif=%s", dev
);
334 audit_log_format(audit_buf
, " %s=%pI4", dir
, &addr
);
335 if (mask_val
!= 0xffffffff) {
337 while (mask_val
> 0) {
341 audit_log_format(audit_buf
, " %s_prefixlen=%d", dir
, mask_len
);
345 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
347 * netlbl_af6list_audit_addr - Audit an IPv6 address
348 * @audit_buf: audit buffer
349 * @src: true if source address, false if destination
350 * @dev: network interface
352 * @mask: IP address mask
355 * Write the IPv6 address and address mask, if necessary, to @audit_buf.
358 void netlbl_af6list_audit_addr(struct audit_buffer
*audit_buf
,
361 const struct in6_addr
*addr
,
362 const struct in6_addr
*mask
)
364 char *dir
= (src
? "src" : "dst");
367 audit_log_format(audit_buf
, " netif=%s", dev
);
368 audit_log_format(audit_buf
, " %s=%pI6", dir
, addr
);
369 if (ntohl(mask
->s6_addr32
[3]) != 0xffffffff) {
373 while (ntohl(mask
->s6_addr32
[++iter
]) == 0xffffffff)
375 mask_val
= ntohl(mask
->s6_addr32
[iter
]);
376 while (mask_val
> 0) {
380 audit_log_format(audit_buf
, " %s_prefixlen=%d", dir
, mask_len
);
384 #endif /* CONFIG_AUDIT */