1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17#include <linux/types.h>
18#include <linux/rcupdate.h>
19#include <linux/list.h>
20#include <linux/spinlock.h>
21#include <linux/in.h>
22#include <linux/in6.h>
23#include <linux/ip.h>
24#include <linux/ipv6.h>
25#include <net/ip.h>
26#include <net/ipv6.h>
27#include <linux/audit.h>
28
29#include "netlabel_addrlist.h"
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46struct netlbl_af4list *netlbl_af4list_search(__be32 addr,
47 struct list_head *head)
48{
49 struct netlbl_af4list *iter;
50
51 list_for_each_entry_rcu(iter, head, list)
52 if (iter->valid && (addr & iter->mask) == iter->addr)
53 return iter;
54
55 return NULL;
56}
57
58
59
60
61
62
63
64
65
66
67
68
69
70struct netlbl_af4list *netlbl_af4list_search_exact(__be32 addr,
71 __be32 mask,
72 struct list_head *head)
73{
74 struct netlbl_af4list *iter;
75
76 list_for_each_entry_rcu(iter, head, list)
77 if (iter->valid && iter->addr == addr && iter->mask == mask)
78 return iter;
79
80 return NULL;
81}
82
83
84#if IS_ENABLED(CONFIG_IPV6)
85
86
87
88
89
90
91
92
93
94
95
96struct netlbl_af6list *netlbl_af6list_search(const struct in6_addr *addr,
97 struct list_head *head)
98{
99 struct netlbl_af6list *iter;
100
101 list_for_each_entry_rcu(iter, head, list)
102 if (iter->valid &&
103 ipv6_masked_addr_cmp(&iter->addr, &iter->mask, addr) == 0)
104 return iter;
105
106 return NULL;
107}
108
109
110
111
112
113
114
115
116
117
118
119
120
121struct netlbl_af6list *netlbl_af6list_search_exact(const struct in6_addr *addr,
122 const struct in6_addr *mask,
123 struct list_head *head)
124{
125 struct netlbl_af6list *iter;
126
127 list_for_each_entry_rcu(iter, head, list)
128 if (iter->valid &&
129 ipv6_addr_equal(&iter->addr, addr) &&
130 ipv6_addr_equal(&iter->mask, mask))
131 return iter;
132
133 return NULL;
134}
135#endif
136
137
138
139
140
141
142
143
144
145
146
147
148int netlbl_af4list_add(struct netlbl_af4list *entry, struct list_head *head)
149{
150 struct netlbl_af4list *iter;
151
152 iter = netlbl_af4list_search(entry->addr, head);
153 if (iter != NULL &&
154 iter->addr == entry->addr && iter->mask == entry->mask)
155 return -EEXIST;
156
157
158
159
160
161 list_for_each_entry_rcu(iter, head, list)
162 if (iter->valid &&
163 ntohl(entry->mask) > ntohl(iter->mask)) {
164 __list_add_rcu(&entry->list,
165 iter->list.prev,
166 &iter->list);
167 return 0;
168 }
169 list_add_tail_rcu(&entry->list, head);
170 return 0;
171}
172
173#if IS_ENABLED(CONFIG_IPV6)
174
175
176
177
178
179
180
181
182
183
184
185int netlbl_af6list_add(struct netlbl_af6list *entry, struct list_head *head)
186{
187 struct netlbl_af6list *iter;
188
189 iter = netlbl_af6list_search(&entry->addr, head);
190 if (iter != NULL &&
191 ipv6_addr_equal(&iter->addr, &entry->addr) &&
192 ipv6_addr_equal(&iter->mask, &entry->mask))
193 return -EEXIST;
194
195
196
197
198
199 list_for_each_entry_rcu(iter, head, list)
200 if (iter->valid &&
201 ipv6_addr_cmp(&entry->mask, &iter->mask) > 0) {
202 __list_add_rcu(&entry->list,
203 iter->list.prev,
204 &iter->list);
205 return 0;
206 }
207 list_add_tail_rcu(&entry->list, head);
208 return 0;
209}
210#endif
211
212
213
214
215
216
217
218
219
220
221void netlbl_af4list_remove_entry(struct netlbl_af4list *entry)
222{
223 entry->valid = 0;
224 list_del_rcu(&entry->list);
225}
226
227
228
229
230
231
232
233
234
235
236
237
238
239struct netlbl_af4list *netlbl_af4list_remove(__be32 addr, __be32 mask,
240 struct list_head *head)
241{
242 struct netlbl_af4list *entry;
243
244 entry = netlbl_af4list_search_exact(addr, mask, head);
245 if (entry == NULL)
246 return NULL;
247 netlbl_af4list_remove_entry(entry);
248 return entry;
249}
250
251#if IS_ENABLED(CONFIG_IPV6)
252
253
254
255
256
257
258
259
260
261void netlbl_af6list_remove_entry(struct netlbl_af6list *entry)
262{
263 entry->valid = 0;
264 list_del_rcu(&entry->list);
265}
266
267
268
269
270
271
272
273
274
275
276
277
278
279struct netlbl_af6list *netlbl_af6list_remove(const struct in6_addr *addr,
280 const struct in6_addr *mask,
281 struct list_head *head)
282{
283 struct netlbl_af6list *entry;
284
285 entry = netlbl_af6list_search_exact(addr, mask, head);
286 if (entry == NULL)
287 return NULL;
288 netlbl_af6list_remove_entry(entry);
289 return entry;
290}
291#endif
292
293
294
295
296
297#ifdef CONFIG_AUDIT
298
299
300
301
302
303
304
305
306
307
308
309
310void netlbl_af4list_audit_addr(struct audit_buffer *audit_buf,
311 int src, const char *dev,
312 __be32 addr, __be32 mask)
313{
314 u32 mask_val = ntohl(mask);
315 char *dir = (src ? "src" : "dst");
316
317 if (dev != NULL)
318 audit_log_format(audit_buf, " netif=%s", dev);
319 audit_log_format(audit_buf, " %s=%pI4", dir, &addr);
320 if (mask_val != 0xffffffff) {
321 u32 mask_len = 0;
322 while (mask_val > 0) {
323 mask_val <<= 1;
324 mask_len++;
325 }
326 audit_log_format(audit_buf, " %s_prefixlen=%d", dir, mask_len);
327 }
328}
329
330#if IS_ENABLED(CONFIG_IPV6)
331
332
333
334
335
336
337
338
339
340
341
342
343void netlbl_af6list_audit_addr(struct audit_buffer *audit_buf,
344 int src,
345 const char *dev,
346 const struct in6_addr *addr,
347 const struct in6_addr *mask)
348{
349 char *dir = (src ? "src" : "dst");
350
351 if (dev != NULL)
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) {
355 u32 mask_len = 0;
356 u32 mask_val;
357 int iter = -1;
358 while (ntohl(mask->s6_addr32[++iter]) == 0xffffffff)
359 mask_len += 32;
360 mask_val = ntohl(mask->s6_addr32[iter]);
361 while (mask_val > 0) {
362 mask_val <<= 1;
363 mask_len++;
364 }
365 audit_log_format(audit_buf, " %s_prefixlen=%d", dir, mask_len);
366 }
367}
368#endif
369#endif
370