1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * NetLabel Network Address Lists
5 * This file contains network address list functions used to manage ordered
6 * lists of network addresses for use by the NetLabel subsystem. The NetLabel
7 * system manages static and dynamic label mappings for network protocols such
10 * Author: Paul Moore <paul@paul-moore.com>
14 * (c) Copyright Hewlett-Packard Development Company, L.P., 2008
17 #include <linux/types.h>
18 #include <linux/rcupdate.h>
19 #include <linux/list.h>
20 #include <linux/spinlock.h>
22 #include <linux/in6.h>
24 #include <linux/ipv6.h>
27 #include <linux/audit.h>
29 #include "netlabel_addrlist.h"
32 * Address List Functions
36 * netlbl_af4list_search - Search for a matching IPv4 address entry
38 * @head: the list head
41 * Searches the IPv4 address list given by @head. If a matching address entry
42 * is found it is returned, otherwise NULL is returned. The caller is
43 * responsible for calling the rcu_read_[un]lock() functions.
46 struct netlbl_af4list
*netlbl_af4list_search(__be32 addr
,
47 struct list_head
*head
)
49 struct netlbl_af4list
*iter
;
51 list_for_each_entry_rcu(iter
, head
, list
)
52 if (iter
->valid
&& (addr
& iter
->mask
) == iter
->addr
)
59 * netlbl_af4list_search_exact - Search for an exact IPv4 address entry
61 * @mask: IPv4 address mask
62 * @head: the list head
65 * Searches the IPv4 address list given by @head. If an exact match if found
66 * it is returned, otherwise NULL is returned. The caller is responsible for
67 * calling the rcu_read_[un]lock() functions.
70 struct netlbl_af4list
*netlbl_af4list_search_exact(__be32 addr
,
72 struct list_head
*head
)
74 struct netlbl_af4list
*iter
;
76 list_for_each_entry_rcu(iter
, head
, list
)
77 if (iter
->valid
&& iter
->addr
== addr
&& iter
->mask
== mask
)
84 #if IS_ENABLED(CONFIG_IPV6)
86 * netlbl_af6list_search - Search for a matching IPv6 address entry
88 * @head: the list head
91 * Searches the IPv6 address list given by @head. If a matching address entry
92 * is found it is returned, otherwise NULL is returned. The caller is
93 * responsible for calling the rcu_read_[un]lock() functions.
96 struct netlbl_af6list
*netlbl_af6list_search(const struct in6_addr
*addr
,
97 struct list_head
*head
)
99 struct netlbl_af6list
*iter
;
101 list_for_each_entry_rcu(iter
, head
, list
)
103 ipv6_masked_addr_cmp(&iter
->addr
, &iter
->mask
, addr
) == 0)
110 * netlbl_af6list_search_exact - Search for an exact IPv6 address entry
111 * @addr: IPv6 address
112 * @mask: IPv6 address mask
113 * @head: the list head
116 * Searches the IPv6 address list given by @head. If an exact match if found
117 * it is returned, otherwise NULL is returned. The caller is responsible for
118 * calling the rcu_read_[un]lock() functions.
121 struct netlbl_af6list
*netlbl_af6list_search_exact(const struct in6_addr
*addr
,
122 const struct in6_addr
*mask
,
123 struct list_head
*head
)
125 struct netlbl_af6list
*iter
;
127 list_for_each_entry_rcu(iter
, head
, list
)
129 ipv6_addr_equal(&iter
->addr
, addr
) &&
130 ipv6_addr_equal(&iter
->mask
, mask
))
138 * netlbl_af4list_add - Add a new IPv4 address entry to a list
139 * @entry: address entry
140 * @head: the list head
143 * Add a new address entry to the list pointed to by @head. On success zero is
144 * returned, otherwise a negative value is returned. The caller is responsible
145 * for calling the necessary locking functions.
148 int netlbl_af4list_add(struct netlbl_af4list
*entry
, struct list_head
*head
)
150 struct netlbl_af4list
*iter
;
152 iter
= netlbl_af4list_search(entry
->addr
, head
);
154 iter
->addr
== entry
->addr
&& iter
->mask
== entry
->mask
)
157 /* in order to speed up address searches through the list (the common
158 * case) we need to keep the list in order based on the size of the
159 * address mask such that the entry with the widest mask (smallest
160 * numerical value) appears first in the list */
161 list_for_each_entry_rcu(iter
, head
, list
)
163 ntohl(entry
->mask
) > ntohl(iter
->mask
)) {
164 __list_add_rcu(&entry
->list
,
169 list_add_tail_rcu(&entry
->list
, head
);
173 #if IS_ENABLED(CONFIG_IPV6)
175 * netlbl_af6list_add - Add a new IPv6 address entry to a list
176 * @entry: address entry
177 * @head: the list head
180 * Add a new address entry to the list pointed to by @head. On success zero is
181 * returned, otherwise a negative value is returned. The caller is responsible
182 * for calling the necessary locking functions.
185 int netlbl_af6list_add(struct netlbl_af6list
*entry
, struct list_head
*head
)
187 struct netlbl_af6list
*iter
;
189 iter
= netlbl_af6list_search(&entry
->addr
, head
);
191 ipv6_addr_equal(&iter
->addr
, &entry
->addr
) &&
192 ipv6_addr_equal(&iter
->mask
, &entry
->mask
))
195 /* in order to speed up address searches through the list (the common
196 * case) we need to keep the list in order based on the size of the
197 * address mask such that the entry with the widest mask (smallest
198 * numerical value) appears first in the list */
199 list_for_each_entry_rcu(iter
, head
, list
)
201 ipv6_addr_cmp(&entry
->mask
, &iter
->mask
) > 0) {
202 __list_add_rcu(&entry
->list
,
207 list_add_tail_rcu(&entry
->list
, head
);
213 * netlbl_af4list_remove_entry - Remove an IPv4 address entry
214 * @entry: address entry
217 * Remove the specified IP address entry. The caller is responsible for
218 * calling the necessary locking functions.
221 void netlbl_af4list_remove_entry(struct netlbl_af4list
*entry
)
224 list_del_rcu(&entry
->list
);
228 * netlbl_af4list_remove - Remove an IPv4 address entry
230 * @mask: IP address mask
231 * @head: the list head
234 * Remove an IP address entry from the list pointed to by @head. Returns the
235 * entry on success, NULL on failure. The caller is responsible for calling
236 * the necessary locking functions.
239 struct netlbl_af4list
*netlbl_af4list_remove(__be32 addr
, __be32 mask
,
240 struct list_head
*head
)
242 struct netlbl_af4list
*entry
;
244 entry
= netlbl_af4list_search_exact(addr
, mask
, head
);
247 netlbl_af4list_remove_entry(entry
);
251 #if IS_ENABLED(CONFIG_IPV6)
253 * netlbl_af6list_remove_entry - Remove an IPv6 address entry
254 * @entry: address entry
257 * Remove the specified IP address entry. The caller is responsible for
258 * calling the necessary locking functions.
261 void netlbl_af6list_remove_entry(struct netlbl_af6list
*entry
)
264 list_del_rcu(&entry
->list
);
268 * netlbl_af6list_remove - Remove an IPv6 address entry
270 * @mask: IP address mask
271 * @head: the list head
274 * Remove an IP address entry from the list pointed to by @head. Returns the
275 * entry on success, NULL on failure. The caller is responsible for calling
276 * the necessary locking functions.
279 struct netlbl_af6list
*netlbl_af6list_remove(const struct in6_addr
*addr
,
280 const struct in6_addr
*mask
,
281 struct list_head
*head
)
283 struct netlbl_af6list
*entry
;
285 entry
= netlbl_af6list_search_exact(addr
, mask
, head
);
288 netlbl_af6list_remove_entry(entry
);
294 * Audit Helper Functions
299 * netlbl_af4list_audit_addr - Audit an IPv4 address
300 * @audit_buf: audit buffer
301 * @src: true if source address, false if destination
302 * @dev: network interface
304 * @mask: IP address mask
307 * Write the IPv4 address and address mask, if necessary, to @audit_buf.
310 void netlbl_af4list_audit_addr(struct audit_buffer
*audit_buf
,
311 int src
, const char *dev
,
312 __be32 addr
, __be32 mask
)
314 u32 mask_val
= ntohl(mask
);
315 char *dir
= (src
? "src" : "dst");
318 audit_log_format(audit_buf
, " netif=%s", dev
);
319 audit_log_format(audit_buf
, " %s=%pI4", dir
, &addr
);
320 if (mask_val
!= 0xffffffff) {
322 while (mask_val
> 0) {
326 audit_log_format(audit_buf
, " %s_prefixlen=%d", dir
, mask_len
);
330 #if IS_ENABLED(CONFIG_IPV6)
332 * netlbl_af6list_audit_addr - Audit an IPv6 address
333 * @audit_buf: audit buffer
334 * @src: true if source address, false if destination
335 * @dev: network interface
337 * @mask: IP address mask
340 * Write the IPv6 address and address mask, if necessary, to @audit_buf.
343 void netlbl_af6list_audit_addr(struct audit_buffer
*audit_buf
,
346 const struct in6_addr
*addr
,
347 const struct in6_addr
*mask
)
349 char *dir
= (src
? "src" : "dst");
352 audit_log_format(audit_buf
, " netif=%s", dev
);
353 audit_log_format(audit_buf
, " %s=%pI6", dir
, addr
);
354 if (ntohl(mask
->s6_addr32
[3]) != 0xffffffff) {
358 while (ntohl(mask
->s6_addr32
[++iter
]) == 0xffffffff)
360 mask_val
= ntohl(mask
->s6_addr32
[iter
]);
361 while (mask_val
> 0) {
365 audit_log_format(audit_buf
, " %s_prefixlen=%d", dir
, mask_len
);
369 #endif /* CONFIG_AUDIT */