netlabel: Add a generic way to create ordered linked lists of network addrs
[safe/jmp/linux-2.6] / net / netlabel / netlabel_addrlist.h
1 /*
2  * NetLabel Network Address Lists
3  *
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
7  * as CIPSO and RIPSO.
8  *
9  * Author: Paul Moore <paul.moore@hp.com>
10  *
11  */
12
13 /*
14  * (c) Copyright Hewlett-Packard Development Company, L.P., 2008
15  *
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
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
29  *
30  */
31
32 #ifndef _NETLABEL_ADDRLIST_H
33 #define _NETLABEL_ADDRLIST_H
34
35 #include <linux/types.h>
36 #include <linux/rcupdate.h>
37 #include <linux/list.h>
38 #include <linux/in6.h>
39
40 /**
41  * struct netlbl_af4list - NetLabel IPv4 address list
42  * @addr: IPv4 address
43  * @mask: IPv4 address mask
44  * @valid: valid flag
45  * @list: list structure, used internally
46  */
47 struct netlbl_af4list {
48         __be32 addr;
49         __be32 mask;
50
51         u32 valid;
52         struct list_head list;
53 };
54
55 /**
56  * struct netlbl_af6list - NetLabel IPv6 address list
57  * @addr: IPv6 address
58  * @mask: IPv6 address mask
59  * @valid: valid flag
60  * @list: list structure, used internally
61  */
62 struct netlbl_af6list {
63         struct in6_addr addr;
64         struct in6_addr mask;
65
66         u32 valid;
67         struct list_head list;
68 };
69
70 #define __af4list_entry(ptr) container_of(ptr, struct netlbl_af4list, list)
71
72 static inline struct netlbl_af4list *__af4list_valid(struct list_head *s,
73                                                      struct list_head *h)
74 {
75         struct list_head *i = s;
76         struct netlbl_af4list *n = __af4list_entry(s);
77         while (i != h && !n->valid) {
78                 i = i->next;
79                 n = __af4list_entry(i);
80         }
81         return n;
82 }
83
84 static inline struct netlbl_af4list *__af4list_valid_rcu(struct list_head *s,
85                                                          struct list_head *h)
86 {
87         struct list_head *i = s;
88         struct netlbl_af4list *n = __af4list_entry(s);
89         while (i != h && !n->valid) {
90                 i = rcu_dereference(i->next);
91                 n = __af4list_entry(i);
92         }
93         return n;
94 }
95
96 #define netlbl_af4list_foreach(iter, head)                              \
97         for (iter = __af4list_valid((head)->next, head);                \
98              prefetch(iter->list.next), &iter->list != (head);          \
99              iter = __af4list_valid(iter->list.next, head))
100
101 #define netlbl_af4list_foreach_rcu(iter, head)                          \
102         for (iter = __af4list_valid_rcu((head)->next, head);            \
103              prefetch(iter->list.next), &iter->list != (head);          \
104              iter = __af4list_valid_rcu(iter->list.next, head))
105
106 #define netlbl_af4list_foreach_safe(iter, tmp, head)                    \
107         for (iter = __af4list_valid((head)->next, head),                \
108                      tmp = __af4list_valid(iter->list.next, head);      \
109              &iter->list != (head);                                     \
110              iter = tmp, tmp = __af4list_valid(iter->list.next, head))
111
112 int netlbl_af4list_add(struct netlbl_af4list *entry,
113                        struct list_head *head);
114 struct netlbl_af4list *netlbl_af4list_remove(__be32 addr, __be32 mask,
115                                              struct list_head *head);
116 void netlbl_af4list_remove_entry(struct netlbl_af4list *entry);
117 struct netlbl_af4list *netlbl_af4list_search(__be32 addr,
118                                              struct list_head *head);
119
120 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
121
122 #define __af6list_entry(ptr) container_of(ptr, struct netlbl_af6list, list)
123
124 static inline struct netlbl_af6list *__af6list_valid(struct list_head *s,
125                                                      struct list_head *h)
126 {
127         struct list_head *i = s;
128         struct netlbl_af6list *n = __af6list_entry(s);
129         while (i != h && !n->valid) {
130                 i = i->next;
131                 n = __af6list_entry(i);
132         }
133         return n;
134 }
135
136 static inline struct netlbl_af6list *__af6list_valid_rcu(struct list_head *s,
137                                                          struct list_head *h)
138 {
139         struct list_head *i = s;
140         struct netlbl_af6list *n = __af6list_entry(s);
141         while (i != h && !n->valid) {
142                 i = rcu_dereference(i->next);
143                 n = __af6list_entry(i);
144         }
145         return n;
146 }
147
148 #define netlbl_af6list_foreach(iter, head)                              \
149         for (iter = __af6list_valid((head)->next, head);                \
150              prefetch(iter->list.next), &iter->list != (head);          \
151              iter = __af6list_valid(iter->list.next, head))
152
153 #define netlbl_af6list_foreach_rcu(iter, head)                          \
154         for (iter = __af6list_valid_rcu((head)->next, head);            \
155              prefetch(iter->list.next), &iter->list != (head);          \
156              iter = __af6list_valid_rcu(iter->list.next, head))
157
158 #define netlbl_af6list_foreach_safe(iter, tmp, head)                    \
159         for (iter = __af6list_valid((head)->next, head),                \
160                      tmp = __af6list_valid(iter->list.next, head);      \
161              &iter->list != (head);                                     \
162              iter = tmp, tmp = __af6list_valid(iter->list.next, head))
163
164 int netlbl_af6list_add(struct netlbl_af6list *entry,
165                        struct list_head *head);
166 struct netlbl_af6list *netlbl_af6list_remove(const struct in6_addr *addr,
167                                              const struct in6_addr *mask,
168                                              struct list_head *head);
169 void netlbl_af6list_remove_entry(struct netlbl_af6list *entry);
170 struct netlbl_af6list *netlbl_af6list_search(const struct in6_addr *addr,
171                                              struct list_head *head);
172 #endif /* IPV6 */
173
174 #endif