162306a36Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0-or-later */
262306a36Sopenharmony_ci/*
362306a36Sopenharmony_ci * NetLabel Network Address Lists
462306a36Sopenharmony_ci *
562306a36Sopenharmony_ci * This file contains network address list functions used to manage ordered
662306a36Sopenharmony_ci * lists of network addresses for use by the NetLabel subsystem.  The NetLabel
762306a36Sopenharmony_ci * system manages static and dynamic label mappings for network protocols such
862306a36Sopenharmony_ci * as CIPSO and RIPSO.
962306a36Sopenharmony_ci *
1062306a36Sopenharmony_ci * Author: Paul Moore <paul@paul-moore.com>
1162306a36Sopenharmony_ci */
1262306a36Sopenharmony_ci
1362306a36Sopenharmony_ci/*
1462306a36Sopenharmony_ci * (c) Copyright Hewlett-Packard Development Company, L.P., 2008
1562306a36Sopenharmony_ci */
1662306a36Sopenharmony_ci
1762306a36Sopenharmony_ci#ifndef _NETLABEL_ADDRLIST_H
1862306a36Sopenharmony_ci#define _NETLABEL_ADDRLIST_H
1962306a36Sopenharmony_ci
2062306a36Sopenharmony_ci#include <linux/types.h>
2162306a36Sopenharmony_ci#include <linux/rcupdate.h>
2262306a36Sopenharmony_ci#include <linux/list.h>
2362306a36Sopenharmony_ci#include <linux/in6.h>
2462306a36Sopenharmony_ci#include <linux/audit.h>
2562306a36Sopenharmony_ci
2662306a36Sopenharmony_ci/**
2762306a36Sopenharmony_ci * struct netlbl_af4list - NetLabel IPv4 address list
2862306a36Sopenharmony_ci * @addr: IPv4 address
2962306a36Sopenharmony_ci * @mask: IPv4 address mask
3062306a36Sopenharmony_ci * @valid: valid flag
3162306a36Sopenharmony_ci * @list: list structure, used internally
3262306a36Sopenharmony_ci */
3362306a36Sopenharmony_cistruct netlbl_af4list {
3462306a36Sopenharmony_ci	__be32 addr;
3562306a36Sopenharmony_ci	__be32 mask;
3662306a36Sopenharmony_ci
3762306a36Sopenharmony_ci	u32 valid;
3862306a36Sopenharmony_ci	struct list_head list;
3962306a36Sopenharmony_ci};
4062306a36Sopenharmony_ci
4162306a36Sopenharmony_ci/**
4262306a36Sopenharmony_ci * struct netlbl_af6list - NetLabel IPv6 address list
4362306a36Sopenharmony_ci * @addr: IPv6 address
4462306a36Sopenharmony_ci * @mask: IPv6 address mask
4562306a36Sopenharmony_ci * @valid: valid flag
4662306a36Sopenharmony_ci * @list: list structure, used internally
4762306a36Sopenharmony_ci */
4862306a36Sopenharmony_cistruct netlbl_af6list {
4962306a36Sopenharmony_ci	struct in6_addr addr;
5062306a36Sopenharmony_ci	struct in6_addr mask;
5162306a36Sopenharmony_ci
5262306a36Sopenharmony_ci	u32 valid;
5362306a36Sopenharmony_ci	struct list_head list;
5462306a36Sopenharmony_ci};
5562306a36Sopenharmony_ci
5662306a36Sopenharmony_ci#define __af4list_entry(ptr) container_of(ptr, struct netlbl_af4list, list)
5762306a36Sopenharmony_ci
5862306a36Sopenharmony_cistatic inline struct netlbl_af4list *__af4list_valid(struct list_head *s,
5962306a36Sopenharmony_ci						     struct list_head *h)
6062306a36Sopenharmony_ci{
6162306a36Sopenharmony_ci	struct list_head *i = s;
6262306a36Sopenharmony_ci	struct netlbl_af4list *n = __af4list_entry(s);
6362306a36Sopenharmony_ci	while (i != h && !n->valid) {
6462306a36Sopenharmony_ci		i = i->next;
6562306a36Sopenharmony_ci		n = __af4list_entry(i);
6662306a36Sopenharmony_ci	}
6762306a36Sopenharmony_ci	return n;
6862306a36Sopenharmony_ci}
6962306a36Sopenharmony_ci
7062306a36Sopenharmony_cistatic inline struct netlbl_af4list *__af4list_valid_rcu(struct list_head *s,
7162306a36Sopenharmony_ci							 struct list_head *h)
7262306a36Sopenharmony_ci{
7362306a36Sopenharmony_ci	struct list_head *i = s;
7462306a36Sopenharmony_ci	struct netlbl_af4list *n = __af4list_entry(s);
7562306a36Sopenharmony_ci	while (i != h && !n->valid) {
7662306a36Sopenharmony_ci		i = rcu_dereference(list_next_rcu(i));
7762306a36Sopenharmony_ci		n = __af4list_entry(i);
7862306a36Sopenharmony_ci	}
7962306a36Sopenharmony_ci	return n;
8062306a36Sopenharmony_ci}
8162306a36Sopenharmony_ci
8262306a36Sopenharmony_ci#define netlbl_af4list_foreach(iter, head)				\
8362306a36Sopenharmony_ci	for (iter = __af4list_valid((head)->next, head);		\
8462306a36Sopenharmony_ci	     &iter->list != (head);					\
8562306a36Sopenharmony_ci	     iter = __af4list_valid(iter->list.next, head))
8662306a36Sopenharmony_ci
8762306a36Sopenharmony_ci#define netlbl_af4list_foreach_rcu(iter, head)				\
8862306a36Sopenharmony_ci	for (iter = __af4list_valid_rcu((head)->next, head);		\
8962306a36Sopenharmony_ci	     &iter->list != (head);					\
9062306a36Sopenharmony_ci	     iter = __af4list_valid_rcu(iter->list.next, head))
9162306a36Sopenharmony_ci
9262306a36Sopenharmony_ci#define netlbl_af4list_foreach_safe(iter, tmp, head)			\
9362306a36Sopenharmony_ci	for (iter = __af4list_valid((head)->next, head),		\
9462306a36Sopenharmony_ci		     tmp = __af4list_valid(iter->list.next, head);	\
9562306a36Sopenharmony_ci	     &iter->list != (head);					\
9662306a36Sopenharmony_ci	     iter = tmp, tmp = __af4list_valid(iter->list.next, head))
9762306a36Sopenharmony_ci
9862306a36Sopenharmony_ciint netlbl_af4list_add(struct netlbl_af4list *entry,
9962306a36Sopenharmony_ci		       struct list_head *head);
10062306a36Sopenharmony_cistruct netlbl_af4list *netlbl_af4list_remove(__be32 addr, __be32 mask,
10162306a36Sopenharmony_ci					     struct list_head *head);
10262306a36Sopenharmony_civoid netlbl_af4list_remove_entry(struct netlbl_af4list *entry);
10362306a36Sopenharmony_cistruct netlbl_af4list *netlbl_af4list_search(__be32 addr,
10462306a36Sopenharmony_ci					     struct list_head *head);
10562306a36Sopenharmony_cistruct netlbl_af4list *netlbl_af4list_search_exact(__be32 addr,
10662306a36Sopenharmony_ci						   __be32 mask,
10762306a36Sopenharmony_ci						   struct list_head *head);
10862306a36Sopenharmony_ci
10962306a36Sopenharmony_ci#ifdef CONFIG_AUDIT
11062306a36Sopenharmony_civoid netlbl_af4list_audit_addr(struct audit_buffer *audit_buf,
11162306a36Sopenharmony_ci			       int src, const char *dev,
11262306a36Sopenharmony_ci			       __be32 addr, __be32 mask);
11362306a36Sopenharmony_ci#else
11462306a36Sopenharmony_cistatic inline void netlbl_af4list_audit_addr(struct audit_buffer *audit_buf,
11562306a36Sopenharmony_ci					     int src, const char *dev,
11662306a36Sopenharmony_ci					     __be32 addr, __be32 mask)
11762306a36Sopenharmony_ci{
11862306a36Sopenharmony_ci}
11962306a36Sopenharmony_ci#endif
12062306a36Sopenharmony_ci
12162306a36Sopenharmony_ci#if IS_ENABLED(CONFIG_IPV6)
12262306a36Sopenharmony_ci
12362306a36Sopenharmony_ci#define __af6list_entry(ptr) container_of(ptr, struct netlbl_af6list, list)
12462306a36Sopenharmony_ci
12562306a36Sopenharmony_cistatic inline struct netlbl_af6list *__af6list_valid(struct list_head *s,
12662306a36Sopenharmony_ci						     struct list_head *h)
12762306a36Sopenharmony_ci{
12862306a36Sopenharmony_ci	struct list_head *i = s;
12962306a36Sopenharmony_ci	struct netlbl_af6list *n = __af6list_entry(s);
13062306a36Sopenharmony_ci	while (i != h && !n->valid) {
13162306a36Sopenharmony_ci		i = i->next;
13262306a36Sopenharmony_ci		n = __af6list_entry(i);
13362306a36Sopenharmony_ci	}
13462306a36Sopenharmony_ci	return n;
13562306a36Sopenharmony_ci}
13662306a36Sopenharmony_ci
13762306a36Sopenharmony_cistatic inline struct netlbl_af6list *__af6list_valid_rcu(struct list_head *s,
13862306a36Sopenharmony_ci							 struct list_head *h)
13962306a36Sopenharmony_ci{
14062306a36Sopenharmony_ci	struct list_head *i = s;
14162306a36Sopenharmony_ci	struct netlbl_af6list *n = __af6list_entry(s);
14262306a36Sopenharmony_ci	while (i != h && !n->valid) {
14362306a36Sopenharmony_ci		i = rcu_dereference(list_next_rcu(i));
14462306a36Sopenharmony_ci		n = __af6list_entry(i);
14562306a36Sopenharmony_ci	}
14662306a36Sopenharmony_ci	return n;
14762306a36Sopenharmony_ci}
14862306a36Sopenharmony_ci
14962306a36Sopenharmony_ci#define netlbl_af6list_foreach(iter, head)				\
15062306a36Sopenharmony_ci	for (iter = __af6list_valid((head)->next, head);		\
15162306a36Sopenharmony_ci	     &iter->list != (head);					\
15262306a36Sopenharmony_ci	     iter = __af6list_valid(iter->list.next, head))
15362306a36Sopenharmony_ci
15462306a36Sopenharmony_ci#define netlbl_af6list_foreach_rcu(iter, head)				\
15562306a36Sopenharmony_ci	for (iter = __af6list_valid_rcu((head)->next, head);		\
15662306a36Sopenharmony_ci	     &iter->list != (head);					\
15762306a36Sopenharmony_ci	     iter = __af6list_valid_rcu(iter->list.next, head))
15862306a36Sopenharmony_ci
15962306a36Sopenharmony_ci#define netlbl_af6list_foreach_safe(iter, tmp, head)			\
16062306a36Sopenharmony_ci	for (iter = __af6list_valid((head)->next, head),		\
16162306a36Sopenharmony_ci		     tmp = __af6list_valid(iter->list.next, head);	\
16262306a36Sopenharmony_ci	     &iter->list != (head);					\
16362306a36Sopenharmony_ci	     iter = tmp, tmp = __af6list_valid(iter->list.next, head))
16462306a36Sopenharmony_ci
16562306a36Sopenharmony_ciint netlbl_af6list_add(struct netlbl_af6list *entry,
16662306a36Sopenharmony_ci		       struct list_head *head);
16762306a36Sopenharmony_cistruct netlbl_af6list *netlbl_af6list_remove(const struct in6_addr *addr,
16862306a36Sopenharmony_ci					     const struct in6_addr *mask,
16962306a36Sopenharmony_ci					     struct list_head *head);
17062306a36Sopenharmony_civoid netlbl_af6list_remove_entry(struct netlbl_af6list *entry);
17162306a36Sopenharmony_cistruct netlbl_af6list *netlbl_af6list_search(const struct in6_addr *addr,
17262306a36Sopenharmony_ci					     struct list_head *head);
17362306a36Sopenharmony_cistruct netlbl_af6list *netlbl_af6list_search_exact(const struct in6_addr *addr,
17462306a36Sopenharmony_ci						   const struct in6_addr *mask,
17562306a36Sopenharmony_ci						   struct list_head *head);
17662306a36Sopenharmony_ci
17762306a36Sopenharmony_ci#ifdef CONFIG_AUDIT
17862306a36Sopenharmony_civoid netlbl_af6list_audit_addr(struct audit_buffer *audit_buf,
17962306a36Sopenharmony_ci			       int src,
18062306a36Sopenharmony_ci			       const char *dev,
18162306a36Sopenharmony_ci			       const struct in6_addr *addr,
18262306a36Sopenharmony_ci			       const struct in6_addr *mask);
18362306a36Sopenharmony_ci#else
18462306a36Sopenharmony_cistatic inline void netlbl_af6list_audit_addr(struct audit_buffer *audit_buf,
18562306a36Sopenharmony_ci					     int src,
18662306a36Sopenharmony_ci					     const char *dev,
18762306a36Sopenharmony_ci					     const struct in6_addr *addr,
18862306a36Sopenharmony_ci					     const struct in6_addr *mask)
18962306a36Sopenharmony_ci{
19062306a36Sopenharmony_ci}
19162306a36Sopenharmony_ci#endif
19262306a36Sopenharmony_ci#endif /* IPV6 */
19362306a36Sopenharmony_ci
19462306a36Sopenharmony_ci#endif
195