ip_pool.c revision 1.2 1 1.2 christos /* $NetBSD: ip_pool.c,v 1.2 2012/03/23 20:39:50 christos Exp $ */
2 1.1 christos
3 1.1 christos /*
4 1.1 christos * Copyright (C) 2012 by Darren Reed.
5 1.1 christos *
6 1.1 christos * See the IPFILTER.LICENCE file for details on licencing.
7 1.1 christos */
8 1.1 christos #if defined(KERNEL) || defined(_KERNEL)
9 1.1 christos # undef KERNEL
10 1.1 christos # undef _KERNEL
11 1.1 christos # define KERNEL 1
12 1.1 christos # define _KERNEL 1
13 1.1 christos #endif
14 1.1 christos #if defined(__osf__)
15 1.1 christos # define _PROTO_NET_H_
16 1.1 christos #endif
17 1.1 christos #include <sys/errno.h>
18 1.1 christos #include <sys/types.h>
19 1.1 christos #include <sys/param.h>
20 1.2 christos #if defined(__NetBSD__)
21 1.2 christos # if (NetBSD >= 199905) && !defined(IPFILTER_LKM) && defined(_KERNEL)
22 1.2 christos # include "opt_ipfilter.h"
23 1.2 christos # endif
24 1.2 christos #endif
25 1.1 christos #include <sys/file.h>
26 1.1 christos #if !defined(_KERNEL) && !defined(__KERNEL__)
27 1.1 christos # include <stdio.h>
28 1.1 christos # include <stdlib.h>
29 1.1 christos # include <string.h>
30 1.1 christos # define _KERNEL
31 1.1 christos # ifdef __OpenBSD__
32 1.1 christos struct file;
33 1.1 christos # endif
34 1.1 christos # include <sys/uio.h>
35 1.1 christos # undef _KERNEL
36 1.1 christos #else
37 1.1 christos # include <sys/systm.h>
38 1.1 christos # if defined(NetBSD) && (__NetBSD_Version__ >= 104000000)
39 1.1 christos # include <sys/proc.h>
40 1.1 christos # endif
41 1.1 christos #endif
42 1.1 christos #include <sys/time.h>
43 1.1 christos #if defined(_KERNEL) && !defined(SOLARIS2)
44 1.1 christos # include <sys/mbuf.h>
45 1.1 christos #endif
46 1.1 christos #if defined(__SVR4) || defined(__svr4__)
47 1.1 christos # include <sys/byteorder.h>
48 1.1 christos # ifdef _KERNEL
49 1.1 christos # include <sys/dditypes.h>
50 1.1 christos # endif
51 1.1 christos # include <sys/stream.h>
52 1.1 christos # include <sys/kmem.h>
53 1.1 christos #endif
54 1.1 christos #if defined(__FreeBSD_version) && (__FreeBSD_version >= 300000)
55 1.1 christos # include <sys/malloc.h>
56 1.1 christos #endif
57 1.1 christos
58 1.1 christos #include <sys/socket.h>
59 1.1 christos #include <net/if.h>
60 1.1 christos #include <netinet/in.h>
61 1.1 christos #if !defined(_KERNEL)
62 1.1 christos # include "ipf.h"
63 1.1 christos #endif
64 1.1 christos
65 1.1 christos #include "netinet/ip_compat.h"
66 1.1 christos #include "netinet/ip_fil.h"
67 1.1 christos #include "netinet/ip_pool.h"
68 1.1 christos #include "netinet/radix_ipf.h"
69 1.1 christos
70 1.1 christos /* END OF INCLUDES */
71 1.1 christos
72 1.1 christos #if !defined(lint)
73 1.2 christos #if defined(__NetBSD__)
74 1.2 christos #include <sys/cdefs.h>
75 1.2 christos __KERNEL_RCSID(0, "$NetBSD: ip_pool.c,v 1.2 2012/03/23 20:39:50 christos Exp $");
76 1.2 christos #else
77 1.1 christos static const char sccsid[] = "@(#)ip_fil.c 2.41 6/5/96 (C) 1993-2000 Darren Reed";
78 1.2 christos static const char rcsid[] = "@(#)Id: ip_pool.c,v 2.97.2.12 2012/01/29 05:30:36 darrenr Exp";
79 1.2 christos #endif
80 1.1 christos #endif
81 1.1 christos
82 1.1 christos typedef struct ipf_pool_softc_s {
83 1.1 christos void *ipf_radix;
84 1.1 christos ip_pool_t *ipf_pool_list[LOOKUP_POOL_SZ];
85 1.1 christos ipf_pool_stat_t ipf_pool_stats;
86 1.1 christos ip_pool_node_t *ipf_node_explist;
87 1.1 christos } ipf_pool_softc_t;
88 1.1 christos
89 1.1 christos
90 1.2 christos static void ipf_pool_clearnodes(ipf_pool_softc_t *, ip_pool_t *);
91 1.2 christos static int ipf_pool_create(ipf_main_softc_t *, ipf_pool_softc_t *, iplookupop_t *);
92 1.2 christos static int ipf_pool_deref(ipf_main_softc_t *, void *, void *);
93 1.2 christos static int ipf_pool_destroy(ipf_main_softc_t *, ipf_pool_softc_t *, int, char *);
94 1.2 christos static void *ipf_pool_exists(ipf_pool_softc_t *, int, char *);
95 1.2 christos static void *ipf_pool_find(void *, int, char *);
96 1.2 christos static ip_pool_node_t *ipf_pool_findeq(ipf_pool_softc_t *, ip_pool_t *,
97 1.2 christos addrfamily_t *, addrfamily_t *);
98 1.2 christos static void ipf_pool_free(ipf_pool_softc_t *, ip_pool_t *);
99 1.2 christos static int ipf_pool_insert_node(ipf_main_softc_t *, ipf_pool_softc_t *,
100 1.2 christos ip_pool_t *, struct ip_pool_node *);
101 1.2 christos static int ipf_pool_iter_deref(ipf_main_softc_t *, void *, int, int, void *);
102 1.2 christos static int ipf_pool_iter_next(ipf_main_softc_t *, void *, ipftoken_t *,
103 1.2 christos ipflookupiter_t *);
104 1.2 christos static size_t ipf_pool_flush(ipf_main_softc_t *, void *, iplookupflush_t *);
105 1.2 christos static int ipf_pool_node_add(ipf_main_softc_t *, void *, iplookupop_t *,
106 1.2 christos int);
107 1.2 christos static int ipf_pool_node_del(ipf_main_softc_t *, void *, iplookupop_t *,
108 1.2 christos int);
109 1.2 christos static void ipf_pool_node_deref(ipf_pool_softc_t *, ip_pool_node_t *);
110 1.2 christos static int ipf_pool_remove_node(ipf_pool_softc_t *, ip_pool_t *,
111 1.2 christos ip_pool_node_t *);
112 1.2 christos static int ipf_pool_search(ipf_main_softc_t *, void *, int,
113 1.2 christos void *, u_int);
114 1.2 christos static void *ipf_pool_soft_create(ipf_main_softc_t *);
115 1.2 christos static void ipf_pool_soft_destroy(ipf_main_softc_t *, void *);
116 1.2 christos static void ipf_pool_soft_fini(ipf_main_softc_t *, void *);
117 1.2 christos static int ipf_pool_soft_init(ipf_main_softc_t *, void *);
118 1.2 christos static int ipf_pool_stats_get(ipf_main_softc_t *, void *, iplookupop_t *);
119 1.2 christos static int ipf_pool_table_add(ipf_main_softc_t *, void *, iplookupop_t *);
120 1.2 christos static int ipf_pool_table_del(ipf_main_softc_t *, void *, iplookupop_t *);
121 1.2 christos static void *ipf_pool_select_add_ref(void *, int, char *);
122 1.2 christos static void ipf_pool_expire(ipf_main_softc_t *, void *);
123 1.1 christos
124 1.1 christos ipf_lookup_t ipf_pool_backend = {
125 1.1 christos IPLT_POOL,
126 1.1 christos ipf_pool_soft_create,
127 1.1 christos ipf_pool_soft_destroy,
128 1.1 christos ipf_pool_soft_init,
129 1.1 christos ipf_pool_soft_fini,
130 1.1 christos ipf_pool_search,
131 1.1 christos ipf_pool_flush,
132 1.1 christos ipf_pool_iter_deref,
133 1.1 christos ipf_pool_iter_next,
134 1.1 christos ipf_pool_node_add,
135 1.1 christos ipf_pool_node_del,
136 1.1 christos ipf_pool_stats_get,
137 1.1 christos ipf_pool_table_add,
138 1.1 christos ipf_pool_table_del,
139 1.1 christos ipf_pool_deref,
140 1.1 christos ipf_pool_find,
141 1.1 christos ipf_pool_select_add_ref,
142 1.1 christos NULL,
143 1.1 christos ipf_pool_expire,
144 1.1 christos NULL
145 1.1 christos };
146 1.1 christos
147 1.1 christos
148 1.1 christos #ifdef TEST_POOL
149 1.2 christos void treeprint(ip_pool_t *);
150 1.1 christos
151 1.1 christos int
152 1.1 christos main(argc, argv)
153 1.1 christos int argc;
154 1.1 christos char *argv[];
155 1.1 christos {
156 1.1 christos ip_pool_node_t node;
157 1.1 christos addrfamily_t a, b;
158 1.1 christos iplookupop_t op;
159 1.1 christos ip_pool_t *ipo;
160 1.1 christos i6addr_t ip;
161 1.1 christos
162 1.1 christos RWLOCK_INIT(softc->ipf_poolrw, "poolrw");
163 1.1 christos ipf_pool_init();
164 1.1 christos
165 1.1 christos bzero((char *)&ip, sizeof(ip));
166 1.1 christos bzero((char *)&op, sizeof(op));
167 1.1 christos bzero((char *)&node, sizeof(node));
168 1.2 christos strlcpy(op.iplo_name, "0", sizeof(op.iplo_name));
169 1.1 christos
170 1.1 christos if (ipf_pool_create(&op) == 0)
171 1.1 christos ipo = ipf_pool_exists(0, "0");
172 1.1 christos
173 1.1 christos node.ipn_addr.adf_family = AF_INET;
174 1.1 christos node.ipn_mask.adf_family = AF_INET;
175 1.1 christos
176 1.1 christos node.ipn_addr.adf_addr.in4.s_addr = 0x0a010203;
177 1.1 christos node.ipn_mask.adf_addr.in4.s_addr = 0xffffffff;
178 1.1 christos node.ipn_info = 1;
179 1.1 christos ipf_pool_insert_node(ipo, &node);
180 1.1 christos
181 1.1 christos node.ipn_addr.adf_addr.in4.s_addr = 0x0a000000;
182 1.1 christos node.ipn_mask.adf_addr.in4.s_addr = 0xff000000;
183 1.1 christos node.ipn_info = 0;
184 1.1 christos ipf_pool_insert_node(ipo, &node);
185 1.1 christos
186 1.1 christos node.ipn_addr.adf_addr.in4.s_addr = 0x0a010100;
187 1.1 christos node.ipn_mask.adf_addr.in4.s_addr = 0xffffff00;
188 1.1 christos node.ipn_info = 1;
189 1.1 christos ipf_pool_insert_node(ipo, &node);
190 1.1 christos
191 1.1 christos node.ipn_addr.adf_addr.in4.s_addr = 0x0a010200;
192 1.1 christos node.ipn_mask.adf_addr.in4.s_addr = 0xffffff00;
193 1.1 christos node.ipn_info = 0;
194 1.1 christos ipf_pool_insert_node(ipo, &node);
195 1.1 christos
196 1.1 christos node.ipn_addr.adf_addr.in4.s_addr = 0x0a010000;
197 1.1 christos node.ipn_mask.adf_addr.in4.s_addr = 0xffff0000;
198 1.1 christos node.ipn_info = 1;
199 1.1 christos ipf_pool_insert_node(ipo, &node);
200 1.1 christos
201 1.1 christos node.ipn_addr.adf_addr.in4.s_addr = 0x0a01020f;
202 1.1 christos node.ipn_mask.adf_addr.in4.s_addr = 0xffffffff;
203 1.1 christos node.ipn_info = 1;
204 1.1 christos ipf_pool_insert_node(ipo, &node);
205 1.1 christos #ifdef DEBUG_POOL
206 1.1 christos treeprint(ipo);
207 1.1 christos #endif
208 1.1 christos ip.in4.s_addr = 0x0a00aabb;
209 1.1 christos printf("search(%#x) = %d (0)\n", ip.in4.s_addr,
210 1.1 christos ipf_pool_search(ipo, 4, &ip, 1));
211 1.1 christos
212 1.1 christos ip.in4.s_addr = 0x0a000001;
213 1.1 christos printf("search(%#x) = %d (0)\n", ip.in4.s_addr,
214 1.1 christos ipf_pool_search(ipo, 4, &ip, 1));
215 1.1 christos
216 1.1 christos ip.in4.s_addr = 0x0a000101;
217 1.1 christos printf("search(%#x) = %d (0)\n", ip.in4.s_addr,
218 1.1 christos ipf_pool_search(ipo, 4, &ip, 1));
219 1.1 christos
220 1.1 christos ip.in4.s_addr = 0x0a010001;
221 1.1 christos printf("search(%#x) = %d (1)\n", ip.in4.s_addr,
222 1.1 christos ipf_pool_search(ipo, 4, &ip, 1));
223 1.1 christos
224 1.1 christos ip.in4.s_addr = 0x0a010101;
225 1.1 christos printf("search(%#x) = %d (1)\n", ip.in4.s_addr,
226 1.1 christos ipf_pool_search(ipo, 4, &ip, 1));
227 1.1 christos
228 1.1 christos ip.in4.s_addr = 0x0a010201;
229 1.1 christos printf("search(%#x) = %d (0)\n", ip.in4.s_addr,
230 1.1 christos ipf_pool_search(ipo, 4, &ip, 1));
231 1.1 christos
232 1.1 christos ip.in4.s_addr = 0x0a010203;
233 1.1 christos printf("search(%#x) = %d (1)\n", ip.in4.s_addr,
234 1.1 christos ipf_pool_search(ipo, 4, &ip, 1));
235 1.1 christos
236 1.1 christos ip.in4.s_addr = 0x0a01020f;
237 1.1 christos printf("search(%#x) = %d (1)\n", ip.in4.s_addr,
238 1.1 christos ipf_pool_search(ipo, 4, &ip, 1));
239 1.1 christos
240 1.1 christos ip.in4.s_addr = 0x0b00aabb;
241 1.1 christos printf("search(%#x) = %d (-1)\n", ip.in4.s_addr,
242 1.1 christos ipf_pool_search(ipo, 4, &ip, 1));
243 1.1 christos
244 1.1 christos #ifdef DEBUG_POOL
245 1.1 christos treeprint(ipo);
246 1.1 christos #endif
247 1.1 christos
248 1.1 christos ipf_pool_fini();
249 1.1 christos
250 1.1 christos return 0;
251 1.1 christos }
252 1.1 christos
253 1.1 christos
254 1.1 christos void
255 1.1 christos treeprint(ipo)
256 1.1 christos ip_pool_t *ipo;
257 1.1 christos {
258 1.1 christos ip_pool_node_t *c;
259 1.1 christos
260 1.1 christos for (c = ipo->ipo_list; c != NULL; c = c->ipn_next)
261 1.1 christos printf("Node %p(%s) (%#x/%#x) = %d hits %lu\n",
262 1.1 christos c, c->ipn_name, c->ipn_addr.adf_addr.in4.s_addr,
263 1.1 christos c->ipn_mask.adf_addr.in4.s_addr,
264 1.1 christos c->ipn_info, c->ipn_hits);
265 1.1 christos }
266 1.1 christos #endif /* TEST_POOL */
267 1.1 christos
268 1.1 christos
269 1.1 christos /* ------------------------------------------------------------------------ */
270 1.1 christos /* Function: ipf_pool_soft_create */
271 1.1 christos /* Returns: void * - NULL = failure, else pointer to local context */
272 1.1 christos /* Parameters: softc(I) - pointer to soft context main structure */
273 1.1 christos /* */
274 1.1 christos /* Initialise the routing table data structures where required. */
275 1.1 christos /* ------------------------------------------------------------------------ */
276 1.1 christos static void *
277 1.2 christos ipf_pool_soft_create(ipf_main_softc_t *softc)
278 1.1 christos {
279 1.1 christos ipf_pool_softc_t *softp;
280 1.1 christos
281 1.1 christos KMALLOC(softp, ipf_pool_softc_t *);
282 1.1 christos if (softc == NULL)
283 1.1 christos return NULL;
284 1.1 christos
285 1.1 christos bzero((char *)softp, sizeof(*softp));
286 1.1 christos
287 1.1 christos softp->ipf_radix = ipf_rx_create();
288 1.1 christos if (softp->ipf_radix == NULL) {
289 1.1 christos KFREE(softp);
290 1.1 christos return NULL;
291 1.1 christos }
292 1.1 christos
293 1.1 christos return softp;
294 1.1 christos }
295 1.1 christos
296 1.1 christos
297 1.1 christos /* ------------------------------------------------------------------------ */
298 1.1 christos /* Function: ipf_pool_soft_init */
299 1.1 christos /* Returns: int - 0 = success, else error */
300 1.1 christos /* Parameters: softc(I) - pointer to soft context main structure */
301 1.1 christos /* arg(I) - pointer to local context to use */
302 1.1 christos /* */
303 1.1 christos /* Initialise the routing table data structures where required. */
304 1.1 christos /* ------------------------------------------------------------------------ */
305 1.1 christos static int
306 1.2 christos ipf_pool_soft_init(ipf_main_softc_t *softc, void *arg)
307 1.1 christos {
308 1.1 christos ipf_pool_softc_t *softp = arg;
309 1.1 christos
310 1.1 christos ipf_rx_init(softp->ipf_radix);
311 1.1 christos
312 1.1 christos return 0;
313 1.1 christos }
314 1.1 christos
315 1.1 christos
316 1.1 christos /* ------------------------------------------------------------------------ */
317 1.1 christos /* Function: ipf_pool_soft_fini */
318 1.1 christos /* Returns: Nil */
319 1.1 christos /* Parameters: softc(I) - pointer to soft context main structure */
320 1.1 christos /* arg(I) - pointer to local context to use */
321 1.1 christos /* Locks: WRITE(ipf_global) */
322 1.1 christos /* */
323 1.1 christos /* Clean up all the pool data structures allocated and call the cleanup */
324 1.1 christos /* function for the radix tree that supports the pools. ipf_pool_destroy is */
325 1.1 christos /* used to delete the pools one by one to ensure they're properly freed up. */
326 1.1 christos /* ------------------------------------------------------------------------ */
327 1.1 christos static void
328 1.2 christos ipf_pool_soft_fini(ipf_main_softc_t *softc, void *arg)
329 1.1 christos {
330 1.1 christos ipf_pool_softc_t *softp = arg;
331 1.1 christos ip_pool_t *p, *q;
332 1.1 christos int i;
333 1.1 christos
334 1.1 christos softc = arg;
335 1.1 christos
336 1.1 christos for (i = -1; i <= IPL_LOGMAX; i++) {
337 1.1 christos for (q = softp->ipf_pool_list[i + 1]; (p = q) != NULL; ) {
338 1.1 christos q = p->ipo_next;
339 1.1 christos (void) ipf_pool_destroy(softc, arg, i, p->ipo_name);
340 1.1 christos }
341 1.1 christos }
342 1.1 christos
343 1.1 christos }
344 1.1 christos
345 1.1 christos
346 1.1 christos /* ------------------------------------------------------------------------ */
347 1.1 christos /* Function: ipf_pool_soft_destroy */
348 1.1 christos /* Returns: Nil */
349 1.1 christos /* Parameters: softc(I) - pointer to soft context main structure */
350 1.1 christos /* arg(I) - pointer to local context to use */
351 1.1 christos /* */
352 1.1 christos /* Clean up the pool by free'ing the radix tree associated with it and free */
353 1.1 christos /* up the pool context too. */
354 1.1 christos /* ------------------------------------------------------------------------ */
355 1.1 christos static void
356 1.2 christos ipf_pool_soft_destroy(ipf_main_softc_t *softc, void *arg)
357 1.1 christos {
358 1.1 christos ipf_pool_softc_t *softp = arg;
359 1.1 christos
360 1.1 christos ipf_rx_destroy(softp->ipf_radix);
361 1.1 christos
362 1.1 christos KFREE(softp);
363 1.1 christos }
364 1.1 christos
365 1.1 christos
366 1.1 christos /* ------------------------------------------------------------------------ */
367 1.1 christos /* Function: ipf_pool_node_add */
368 1.1 christos /* Returns: int - 0 = success, else error */
369 1.1 christos /* Parameters: softc(I) - pointer to soft context main structure */
370 1.1 christos /* arg(I) - pointer to local context to use */
371 1.1 christos /* op(I) - pointer to lookup operatin data */
372 1.1 christos /* */
373 1.1 christos /* ------------------------------------------------------------------------ */
374 1.1 christos static int
375 1.2 christos ipf_pool_node_add(ipf_main_softc_t *softc, void *arg, iplookupop_t *op, int uid)
376 1.1 christos {
377 1.1 christos ip_pool_node_t node, *m;
378 1.1 christos ip_pool_t *p;
379 1.1 christos int err;
380 1.1 christos
381 1.1 christos if (op->iplo_size != sizeof(node)) {
382 1.1 christos IPFERROR(70014);
383 1.1 christos return EINVAL;
384 1.1 christos }
385 1.1 christos
386 1.1 christos err = COPYIN(op->iplo_struct, &node, sizeof(node));
387 1.1 christos if (err != 0) {
388 1.1 christos IPFERROR(70015);
389 1.1 christos return EFAULT;
390 1.1 christos }
391 1.1 christos
392 1.1 christos if (node.ipn_addr.adf_family != node.ipn_mask.adf_family) {
393 1.1 christos IPFERROR(70016);
394 1.1 christos return EINVAL;
395 1.1 christos }
396 1.1 christos
397 1.1 christos p = ipf_pool_find(arg, op->iplo_unit, op->iplo_name);
398 1.1 christos if (p == NULL) {
399 1.1 christos IPFERROR(70017);
400 1.1 christos return ESRCH;
401 1.1 christos }
402 1.1 christos
403 1.1 christos /*
404 1.1 christos * add an entry to a pool - return an error if it already
405 1.1 christos * exists remove an entry from a pool - if it exists
406 1.1 christos * - in both cases, the pool *must* exist!
407 1.1 christos */
408 1.1 christos m = ipf_pool_findeq(arg, p, &node.ipn_addr, &node.ipn_mask);
409 1.1 christos if (m != NULL) {
410 1.1 christos IPFERROR(70018);
411 1.1 christos return EEXIST;
412 1.1 christos }
413 1.1 christos err = ipf_pool_insert_node(softc, arg, p, &node);
414 1.1 christos
415 1.1 christos return err;
416 1.1 christos }
417 1.1 christos
418 1.1 christos
419 1.1 christos /* ------------------------------------------------------------------------ */
420 1.1 christos /* Function: ipf_pool_node_del */
421 1.1 christos /* Returns: int - 0 = success, else error */
422 1.1 christos /* Parameters: softc(I) - pointer to soft context main structure */
423 1.1 christos /* arg(I) - pointer to local context to use */
424 1.1 christos /* op(I) - pointer to lookup operatin data */
425 1.1 christos /* */
426 1.1 christos /* ------------------------------------------------------------------------ */
427 1.1 christos static int
428 1.2 christos ipf_pool_node_del(ipf_main_softc_t *softc, void *arg, iplookupop_t *op, int uid)
429 1.1 christos {
430 1.1 christos ip_pool_node_t node, *m;
431 1.1 christos ip_pool_t *p;
432 1.1 christos int err;
433 1.1 christos
434 1.1 christos
435 1.1 christos if (op->iplo_size != sizeof(node)) {
436 1.1 christos IPFERROR(70019);
437 1.1 christos return EINVAL;
438 1.1 christos }
439 1.1 christos node.ipn_uid = uid;
440 1.1 christos
441 1.1 christos err = COPYIN(op->iplo_struct, &node, sizeof(node));
442 1.1 christos if (err != 0) {
443 1.1 christos IPFERROR(70020);
444 1.1 christos return EFAULT;
445 1.1 christos }
446 1.1 christos
447 1.1 christos p = ipf_pool_find(arg, op->iplo_unit, op->iplo_name);
448 1.1 christos if (p == NULL) {
449 1.1 christos IPFERROR(70021);
450 1.1 christos return ESRCH;
451 1.1 christos }
452 1.1 christos
453 1.1 christos m = ipf_pool_findeq(arg, p, &node.ipn_addr, &node.ipn_mask);
454 1.1 christos if (m == NULL) {
455 1.1 christos IPFERROR(70022);
456 1.1 christos return ENOENT;
457 1.1 christos }
458 1.1 christos
459 1.1 christos if ((uid != 0) && (uid != m->ipn_uid)) {
460 1.1 christos IPFERROR(70024);
461 1.1 christos return EACCES;
462 1.1 christos }
463 1.1 christos
464 1.1 christos err = ipf_pool_remove_node(arg, p, m);
465 1.1 christos
466 1.1 christos return err;
467 1.1 christos }
468 1.1 christos
469 1.1 christos
470 1.1 christos /* ------------------------------------------------------------------------ */
471 1.1 christos /* Function: ipf_pool_table_add */
472 1.1 christos /* Returns: int - 0 = success, else error */
473 1.1 christos /* Parameters: softc(I) - pointer to soft context main structure */
474 1.1 christos /* arg(I) - pointer to local context to use */
475 1.1 christos /* op(I) - pointer to lookup operatin data */
476 1.1 christos /* */
477 1.1 christos /* ------------------------------------------------------------------------ */
478 1.1 christos static int
479 1.2 christos ipf_pool_table_add(ipf_main_softc_t *softc, void *arg, iplookupop_t *op)
480 1.1 christos {
481 1.1 christos int err;
482 1.1 christos
483 1.1 christos if (((op->iplo_arg & LOOKUP_ANON) == 0) &&
484 1.1 christos (ipf_pool_find(arg, op->iplo_unit, op->iplo_name) != NULL)) {
485 1.1 christos IPFERROR(70023);
486 1.1 christos err = EEXIST;
487 1.1 christos } else {
488 1.1 christos err = ipf_pool_create(softc, arg, op);
489 1.1 christos }
490 1.1 christos
491 1.1 christos return err;
492 1.1 christos }
493 1.1 christos
494 1.1 christos
495 1.1 christos /* ------------------------------------------------------------------------ */
496 1.1 christos /* Function: ipf_pool_table_del */
497 1.1 christos /* Returns: int - 0 = success, else error */
498 1.1 christos /* Parameters: softc(I) - pointer to soft context main structure */
499 1.1 christos /* arg(I) - pointer to local context to use */
500 1.1 christos /* op(I) - pointer to lookup operatin data */
501 1.1 christos /* */
502 1.1 christos /* ------------------------------------------------------------------------ */
503 1.1 christos static int
504 1.2 christos ipf_pool_table_del(ipf_main_softc_t *softc, void *arg, iplookupop_t *op)
505 1.1 christos {
506 1.1 christos return ipf_pool_destroy(softc, arg, op->iplo_unit, op->iplo_name);
507 1.1 christos }
508 1.1 christos
509 1.1 christos
510 1.1 christos /* ------------------------------------------------------------------------ */
511 1.1 christos /* Function: ipf_pool_statistics */
512 1.1 christos /* Returns: int - 0 = success, else error */
513 1.1 christos /* Parameters: softc(I) - pointer to soft context main structure */
514 1.1 christos /* arg(I) - pointer to local context to use */
515 1.1 christos /* op(I) - pointer to lookup operatin data */
516 1.1 christos /* */
517 1.1 christos /* Copy the current statistics out into user space, collecting pool list */
518 1.1 christos /* pointers as appropriate for later use. */
519 1.1 christos /* ------------------------------------------------------------------------ */
520 1.1 christos static int
521 1.2 christos ipf_pool_stats_get(ipf_main_softc_t *softc, void *arg, iplookupop_t *op)
522 1.1 christos {
523 1.1 christos ipf_pool_softc_t *softp = arg;
524 1.1 christos ipf_pool_stat_t stats;
525 1.1 christos int unit, i, err = 0;
526 1.1 christos
527 1.1 christos if (op->iplo_size != sizeof(ipf_pool_stat_t)) {
528 1.1 christos IPFERROR(70001);
529 1.1 christos return EINVAL;
530 1.1 christos }
531 1.1 christos
532 1.1 christos bcopy((char *)&softp->ipf_pool_stats, (char *)&stats, sizeof(stats));
533 1.1 christos unit = op->iplo_unit;
534 1.1 christos if (unit == IPL_LOGALL) {
535 1.1 christos for (i = 0; i <= LOOKUP_POOL_MAX; i++)
536 1.1 christos stats.ipls_list[i] = softp->ipf_pool_list[i];
537 1.1 christos } else if (unit >= 0 && unit <= IPL_LOGMAX) {
538 1.1 christos unit++; /* -1 => 0 */
539 1.1 christos if (op->iplo_name[0] != '\0')
540 1.1 christos stats.ipls_list[unit] = ipf_pool_exists(softp, unit - 1,
541 1.1 christos op->iplo_name);
542 1.1 christos else
543 1.1 christos stats.ipls_list[unit] = softp->ipf_pool_list[unit];
544 1.1 christos } else {
545 1.1 christos IPFERROR(70025);
546 1.1 christos err = EINVAL;
547 1.1 christos }
548 1.1 christos if (err == 0) {
549 1.1 christos err = COPYOUT(&stats, op->iplo_struct, sizeof(stats));
550 1.1 christos if (err != 0) {
551 1.1 christos IPFERROR(70026);
552 1.1 christos return EFAULT;
553 1.1 christos }
554 1.1 christos }
555 1.1 christos return 0;
556 1.1 christos }
557 1.1 christos
558 1.1 christos
559 1.1 christos /* ------------------------------------------------------------------------ */
560 1.1 christos /* Function: ipf_pool_exists */
561 1.1 christos /* Returns: int - 0 = success, else error */
562 1.1 christos /* Parameters: softp(I) - pointer to soft context pool information */
563 1.1 christos /* unit(I) - ipfilter device to which we are working on */
564 1.1 christos /* name(I) - name of the pool */
565 1.1 christos /* */
566 1.1 christos /* Find a matching pool inside the collection of pools for a particular */
567 1.1 christos /* device, indicated by the unit number. */
568 1.1 christos /* ------------------------------------------------------------------------ */
569 1.1 christos static void *
570 1.2 christos ipf_pool_exists(ipf_pool_softc_t *softp, int unit, char *name)
571 1.1 christos {
572 1.1 christos ip_pool_t *p;
573 1.1 christos int i;
574 1.1 christos
575 1.1 christos if (unit == IPL_LOGALL) {
576 1.1 christos for (i = 0; i <= LOOKUP_POOL_MAX; i++) {
577 1.1 christos for (p = softp->ipf_pool_list[i]; p != NULL;
578 1.1 christos p = p->ipo_next) {
579 1.1 christos if (strncmp(p->ipo_name, name,
580 1.1 christos sizeof(p->ipo_name)) == 0)
581 1.1 christos break;
582 1.1 christos }
583 1.1 christos if (p != NULL)
584 1.1 christos break;
585 1.1 christos }
586 1.1 christos } else {
587 1.1 christos for (p = softp->ipf_pool_list[unit + 1]; p != NULL;
588 1.1 christos p = p->ipo_next)
589 1.1 christos if (strncmp(p->ipo_name, name,
590 1.1 christos sizeof(p->ipo_name)) == 0)
591 1.1 christos break;
592 1.1 christos }
593 1.1 christos return p;
594 1.1 christos }
595 1.1 christos
596 1.1 christos
597 1.1 christos /* ------------------------------------------------------------------------ */
598 1.1 christos /* Function: ipf_pool_find */
599 1.1 christos /* Returns: int - 0 = success, else error */
600 1.1 christos /* Parameters: arg(I) - pointer to local context to use */
601 1.1 christos /* unit(I) - ipfilter device to which we are working on */
602 1.1 christos /* name(I) - name of the pool */
603 1.1 christos /* */
604 1.1 christos /* Find a matching pool inside the collection of pools for a particular */
605 1.1 christos /* device, indicated by the unit number. If it is marked for deletion then */
606 1.1 christos /* pretend it does not exist. */
607 1.1 christos /* ------------------------------------------------------------------------ */
608 1.1 christos static void *
609 1.2 christos ipf_pool_find(void *arg, int unit, char *name)
610 1.1 christos {
611 1.1 christos ipf_pool_softc_t *softp = arg;
612 1.1 christos ip_pool_t *p;
613 1.1 christos
614 1.1 christos p = ipf_pool_exists(softp, unit, name);
615 1.1 christos if ((p != NULL) && (p->ipo_flags & IPOOL_DELETE))
616 1.1 christos return NULL;
617 1.1 christos
618 1.1 christos return p;
619 1.1 christos }
620 1.1 christos
621 1.1 christos
622 1.1 christos /* ------------------------------------------------------------------------ */
623 1.1 christos /* Function: ipf_pool_select_add_ref */
624 1.1 christos /* Returns: int - 0 = success, else error */
625 1.1 christos /* Parameters: arg(I) - pointer to local context to use */
626 1.1 christos /* unit(I) - ipfilter device to which we are working on */
627 1.1 christos /* name(I) - name of the pool */
628 1.1 christos /* */
629 1.1 christos /* ------------------------------------------------------------------------ */
630 1.1 christos static void *
631 1.2 christos ipf_pool_select_add_ref(void *arg, int unit, char *name)
632 1.1 christos {
633 1.1 christos ip_pool_t *p;
634 1.1 christos
635 1.1 christos p = ipf_pool_find(arg, -1, name);
636 1.1 christos if (p == NULL)
637 1.1 christos p = ipf_pool_find(arg, unit, name);
638 1.1 christos if (p != NULL) {
639 1.1 christos ATOMIC_INC32(p->ipo_ref);
640 1.1 christos }
641 1.1 christos return p;
642 1.1 christos }
643 1.1 christos
644 1.1 christos
645 1.1 christos /* ------------------------------------------------------------------------ */
646 1.1 christos /* Function: ipf_pool_findeq */
647 1.1 christos /* Returns: int - 0 = success, else error */
648 1.1 christos /* Parameters: softp(I) - pointer to soft context pool information */
649 1.1 christos /* ipo(I) - pointer to the pool getting the new node. */
650 1.1 christos /* addr(I) - pointer to address information to match on */
651 1.1 christos /* mask(I) - pointer to the address mask to match */
652 1.1 christos /* */
653 1.1 christos /* Searches for an exact match of an entry in the pool. */
654 1.1 christos /* ------------------------------------------------------------------------ */
655 1.2 christos extern void printhostmask(int, u_32_t *, u_32_t *);
656 1.1 christos static ip_pool_node_t *
657 1.2 christos ipf_pool_findeq(ipf_pool_softc_t *softp, ip_pool_t *ipo, addrfamily_t *addr,
658 1.2 christos addrfamily_t *mask)
659 1.1 christos {
660 1.1 christos ipf_rdx_node_t *n;
661 1.1 christos
662 1.1 christos n = ipo->ipo_head->lookup(ipo->ipo_head, addr, mask);
663 1.1 christos return (ip_pool_node_t *)n;
664 1.1 christos }
665 1.1 christos
666 1.1 christos
667 1.1 christos /* ------------------------------------------------------------------------ */
668 1.1 christos /* Function: ipf_pool_search */
669 1.1 christos /* Returns: int - 0 == +ve match, -1 == error, 1 == -ve/no match */
670 1.1 christos /* Parameters: softc(I) - pointer to soft context main structure */
671 1.1 christos /* tptr(I) - pointer to the pool to search */
672 1.1 christos /* version(I) - IP protocol version (4 or 6) */
673 1.1 christos /* dptr(I) - pointer to address information */
674 1.1 christos /* bytes(I) - length of packet */
675 1.1 christos /* */
676 1.1 christos /* Search the pool for a given address and return a search result. */
677 1.1 christos /* ------------------------------------------------------------------------ */
678 1.1 christos static int
679 1.2 christos ipf_pool_search(ipf_main_softc_t *softc, void *tptr, int ipversion, void *dptr,
680 1.2 christos u_int bytes)
681 1.1 christos {
682 1.1 christos ipf_rdx_node_t *rn;
683 1.1 christos ip_pool_node_t *m;
684 1.1 christos i6addr_t *addr;
685 1.1 christos addrfamily_t v;
686 1.1 christos ip_pool_t *ipo;
687 1.1 christos int rv;
688 1.1 christos
689 1.1 christos ipo = tptr;
690 1.1 christos if (ipo == NULL)
691 1.1 christos return -1;
692 1.1 christos
693 1.1 christos rv = 1;
694 1.1 christos m = NULL;
695 1.1 christos addr = (i6addr_t *)dptr;
696 1.1 christos bzero(&v, sizeof(v));
697 1.1 christos v.adf_len = offsetof(addrfamily_t, adf_addr);
698 1.1 christos
699 1.1 christos if (ipversion == 4) {
700 1.1 christos v.adf_family = AF_INET;
701 1.1 christos v.adf_len += sizeof(addr->in4);
702 1.1 christos v.adf_addr.in4 = addr->in4;
703 1.1 christos #ifdef USE_INET6
704 1.1 christos } else if (ipversion == 6) {
705 1.1 christos v.adf_family = AF_INET6;
706 1.1 christos v.adf_len += sizeof(addr->in6);
707 1.1 christos v.adf_addr.in6 = addr->in6;
708 1.1 christos #endif
709 1.1 christos } else
710 1.1 christos return -1;
711 1.1 christos
712 1.1 christos READ_ENTER(&softc->ipf_poolrw);
713 1.1 christos
714 1.1 christos rn = ipo->ipo_head->matchaddr(ipo->ipo_head, &v);
715 1.1 christos
716 1.1 christos if ((rn != NULL) && (rn->root == 0)) {
717 1.1 christos m = (ip_pool_node_t *)rn;
718 1.1 christos ipo->ipo_hits++;
719 1.1 christos m->ipn_bytes += bytes;
720 1.1 christos m->ipn_hits++;
721 1.1 christos rv = m->ipn_info;
722 1.1 christos }
723 1.1 christos RWLOCK_EXIT(&softc->ipf_poolrw);
724 1.1 christos return rv;
725 1.1 christos }
726 1.1 christos
727 1.1 christos
728 1.1 christos /* ------------------------------------------------------------------------ */
729 1.1 christos /* Function: ipf_pool_insert_node */
730 1.1 christos /* Returns: int - 0 = success, else error */
731 1.1 christos /* Parameters: softc(I) - pointer to soft context main structure */
732 1.1 christos /* softp(I) - pointer to soft context pool information */
733 1.1 christos /* ipo(I) - pointer to the pool getting the new node. */
734 1.1 christos /* node(I) - structure with address/mask to add */
735 1.1 christos /* Locks: WRITE(ipf_poolrw) */
736 1.1 christos /* */
737 1.1 christos /* Add another node to the pool given by ipo. The three parameters passed */
738 1.1 christos /* in (addr, mask, info) shold all be stored in the node. */
739 1.1 christos /* ------------------------------------------------------------------------ */
740 1.1 christos static int
741 1.2 christos ipf_pool_insert_node(ipf_main_softc_t *softc, ipf_pool_softc_t *softp,
742 1.2 christos ip_pool_t *ipo, struct ip_pool_node *node)
743 1.1 christos {
744 1.1 christos ipf_rdx_node_t *rn;
745 1.1 christos ip_pool_node_t *x;
746 1.1 christos
747 1.1 christos if ((node->ipn_addr.adf_len > sizeof(*rn)) ||
748 1.1 christos (node->ipn_addr.adf_len < 4)) {
749 1.1 christos IPFERROR(70003);
750 1.1 christos return EINVAL;
751 1.1 christos }
752 1.1 christos
753 1.1 christos if ((node->ipn_mask.adf_len > sizeof(*rn)) ||
754 1.1 christos (node->ipn_mask.adf_len < 4)) {
755 1.1 christos IPFERROR(70004);
756 1.1 christos return EINVAL;
757 1.1 christos }
758 1.1 christos
759 1.1 christos KMALLOC(x, ip_pool_node_t *);
760 1.1 christos if (x == NULL) {
761 1.1 christos IPFERROR(70002);
762 1.1 christos return ENOMEM;
763 1.1 christos }
764 1.1 christos
765 1.1 christos *x = *node;
766 1.1 christos bzero((char *)x->ipn_nodes, sizeof(x->ipn_nodes));
767 1.1 christos x->ipn_owner = ipo;
768 1.1 christos x->ipn_hits = 0;
769 1.1 christos x->ipn_next = NULL;
770 1.1 christos x->ipn_pnext = NULL;
771 1.1 christos x->ipn_dnext = NULL;
772 1.1 christos x->ipn_pdnext = NULL;
773 1.1 christos
774 1.1 christos if (x->ipn_die != 0) {
775 1.1 christos /*
776 1.1 christos * If the new node has a given expiration time, insert it
777 1.1 christos * into the list of expiring nodes with the ones to be
778 1.1 christos * removed first added to the front of the list. The
779 1.1 christos * insertion is O(n) but it is kept sorted for quick scans
780 1.1 christos * at expiration interval checks.
781 1.1 christos */
782 1.1 christos ip_pool_node_t *n;
783 1.1 christos
784 1.1 christos x->ipn_die = softc->ipf_ticks + IPF_TTLVAL(x->ipn_die);
785 1.1 christos for (n = softp->ipf_node_explist; n != NULL; n = n->ipn_dnext) {
786 1.1 christos if (x->ipn_die < n->ipn_die)
787 1.1 christos break;
788 1.1 christos if (n->ipn_dnext == NULL) {
789 1.1 christos /*
790 1.1 christos * We've got to the last node and everything
791 1.1 christos * wanted to be expired before this new node,
792 1.1 christos * so we have to tack it on the end...
793 1.1 christos */
794 1.1 christos n->ipn_dnext = x;
795 1.1 christos x->ipn_pdnext = &n->ipn_dnext;
796 1.1 christos n = NULL;
797 1.1 christos break;
798 1.1 christos }
799 1.1 christos }
800 1.1 christos
801 1.1 christos if (softp->ipf_node_explist == NULL) {
802 1.1 christos softp->ipf_node_explist = x;
803 1.1 christos x->ipn_pdnext = &softp->ipf_node_explist;
804 1.1 christos } else if (n != NULL) {
805 1.1 christos x->ipn_dnext = n;
806 1.1 christos x->ipn_pdnext = n->ipn_pdnext;
807 1.1 christos n->ipn_pdnext = &x->ipn_dnext;
808 1.1 christos }
809 1.1 christos }
810 1.1 christos
811 1.1 christos rn = ipo->ipo_head->addaddr(ipo->ipo_head, &x->ipn_addr, &x->ipn_mask,
812 1.1 christos x->ipn_nodes);
813 1.1 christos #ifdef DEBUG_POOL
814 1.1 christos printf("Added %p at %p\n", x, rn);
815 1.1 christos #endif
816 1.1 christos
817 1.1 christos if (rn == NULL) {
818 1.1 christos KFREE(x);
819 1.1 christos IPFERROR(70005);
820 1.1 christos return ENOMEM;
821 1.1 christos }
822 1.1 christos
823 1.1 christos x->ipn_ref = 1;
824 1.1 christos x->ipn_next = ipo->ipo_list;
825 1.1 christos x->ipn_pnext = &ipo->ipo_list;
826 1.1 christos if (ipo->ipo_list != NULL)
827 1.1 christos ipo->ipo_list->ipn_pnext = &x->ipn_next;
828 1.1 christos ipo->ipo_list = x;
829 1.1 christos
830 1.1 christos softp->ipf_pool_stats.ipls_nodes++;
831 1.1 christos
832 1.1 christos return 0;
833 1.1 christos }
834 1.1 christos
835 1.1 christos
836 1.1 christos /* ------------------------------------------------------------------------ */
837 1.1 christos /* Function: ipf_pool_create */
838 1.1 christos /* Returns: int - 0 = success, else error */
839 1.1 christos /* Parameters: softc(I) - pointer to soft context main structure */
840 1.1 christos /* softp(I) - pointer to soft context pool information */
841 1.1 christos /* op(I) - pointer to iplookup struct with call details */
842 1.1 christos /* Locks: WRITE(ipf_poolrw) */
843 1.1 christos /* */
844 1.1 christos /* Creates a new group according to the paramters passed in via the */
845 1.1 christos /* iplookupop structure. Does not check to see if the group already exists */
846 1.1 christos /* when being inserted - assume this has already been done. If the pool is */
847 1.1 christos /* marked as being anonymous, give it a new, unique, identifier. Call any */
848 1.1 christos /* other functions required to initialise the structure. */
849 1.1 christos /* */
850 1.1 christos /* If the structure is flagged for deletion then reset the flag and return, */
851 1.1 christos /* as this likely means we've tried to free a pool that is in use (flush) */
852 1.1 christos /* and now want to repopulate it with "new" data. */
853 1.1 christos /* ------------------------------------------------------------------------ */
854 1.1 christos static int
855 1.2 christos ipf_pool_create(ipf_main_softc_t *softc, ipf_pool_softc_t *softp,
856 1.2 christos iplookupop_t *op)
857 1.1 christos {
858 1.1 christos char name[FR_GROUPLEN];
859 1.1 christos int poolnum, unit;
860 1.1 christos ip_pool_t *h;
861 1.1 christos
862 1.1 christos unit = op->iplo_unit;
863 1.1 christos
864 1.1 christos if ((op->iplo_arg & LOOKUP_ANON) == 0) {
865 1.1 christos h = ipf_pool_exists(softp, unit, op->iplo_name);
866 1.1 christos if (h != NULL) {
867 1.1 christos if ((h->ipo_flags & IPOOL_DELETE) == 0) {
868 1.1 christos IPFERROR(70006);
869 1.1 christos return EEXIST;
870 1.1 christos }
871 1.1 christos h->ipo_flags &= ~IPOOL_DELETE;
872 1.1 christos return 0;
873 1.1 christos }
874 1.1 christos }
875 1.1 christos
876 1.1 christos KMALLOC(h, ip_pool_t *);
877 1.1 christos if (h == NULL) {
878 1.1 christos IPFERROR(70007);
879 1.1 christos return ENOMEM;
880 1.1 christos }
881 1.1 christos bzero(h, sizeof(*h));
882 1.1 christos
883 1.1 christos if (ipf_rx_inithead(softp->ipf_radix, &h->ipo_head) != 0) {
884 1.1 christos KFREE(h);
885 1.1 christos IPFERROR(70008);
886 1.1 christos return ENOMEM;
887 1.1 christos }
888 1.1 christos
889 1.1 christos if ((op->iplo_arg & LOOKUP_ANON) != 0) {
890 1.1 christos ip_pool_t *p;
891 1.1 christos
892 1.1 christos h->ipo_flags |= IPOOL_ANON;
893 1.1 christos poolnum = LOOKUP_ANON;
894 1.1 christos
895 1.1 christos #if defined(SNPRINTF) && defined(_KERNEL)
896 1.1 christos SNPRINTF(name, sizeof(name), "%x", poolnum);
897 1.1 christos #else
898 1.1 christos (void)sprintf(name, "%x", poolnum);
899 1.1 christos #endif
900 1.1 christos
901 1.1 christos for (p = softp->ipf_pool_list[unit + 1]; p != NULL; ) {
902 1.1 christos if (strncmp(name, p->ipo_name,
903 1.1 christos sizeof(p->ipo_name)) == 0) {
904 1.1 christos poolnum++;
905 1.1 christos #if defined(SNPRINTF) && defined(_KERNEL)
906 1.1 christos SNPRINTF(name, sizeof(name), "%x", poolnum);
907 1.1 christos #else
908 1.1 christos (void)sprintf(name, "%x", poolnum);
909 1.1 christos #endif
910 1.1 christos p = softp->ipf_pool_list[unit + 1];
911 1.1 christos } else
912 1.1 christos p = p->ipo_next;
913 1.1 christos }
914 1.1 christos
915 1.1 christos (void)strncpy(h->ipo_name, name, sizeof(h->ipo_name));
916 1.1 christos (void)strncpy(op->iplo_name, name, sizeof(op->iplo_name));
917 1.1 christos } else {
918 1.1 christos (void)strncpy(h->ipo_name, op->iplo_name, sizeof(h->ipo_name));
919 1.1 christos }
920 1.1 christos
921 1.1 christos h->ipo_radix = softp->ipf_radix;
922 1.1 christos h->ipo_ref = 1;
923 1.1 christos h->ipo_list = NULL;
924 1.1 christos h->ipo_unit = unit;
925 1.1 christos h->ipo_next = softp->ipf_pool_list[unit + 1];
926 1.1 christos if (softp->ipf_pool_list[unit + 1] != NULL)
927 1.1 christos softp->ipf_pool_list[unit + 1]->ipo_pnext = &h->ipo_next;
928 1.1 christos h->ipo_pnext = &softp->ipf_pool_list[unit + 1];
929 1.1 christos softp->ipf_pool_list[unit + 1] = h;
930 1.1 christos
931 1.1 christos softp->ipf_pool_stats.ipls_pools++;
932 1.1 christos
933 1.1 christos return 0;
934 1.1 christos }
935 1.1 christos
936 1.1 christos
937 1.1 christos /* ------------------------------------------------------------------------ */
938 1.1 christos /* Function: ipf_pool_remove_node */
939 1.1 christos /* Returns: int - 0 = success, else error */
940 1.1 christos /* Parameters: softc(I) - pointer to soft context main structure */
941 1.1 christos /* ipo(I) - pointer to the pool to remove the node from. */
942 1.1 christos /* ipe(I) - address being deleted as a node */
943 1.1 christos /* Locks: WRITE(ipf_poolrw) */
944 1.1 christos /* */
945 1.1 christos /* Remove a node from the pool given by ipo. */
946 1.1 christos /* ------------------------------------------------------------------------ */
947 1.1 christos static int
948 1.2 christos ipf_pool_remove_node(ipf_pool_softc_t *softp, ip_pool_t *ipo,
949 1.2 christos ip_pool_node_t *ipe)
950 1.1 christos {
951 1.1 christos
952 1.1 christos if (ipe->ipn_pnext != NULL)
953 1.1 christos *ipe->ipn_pnext = ipe->ipn_next;
954 1.1 christos if (ipe->ipn_next != NULL)
955 1.1 christos ipe->ipn_next->ipn_pnext = ipe->ipn_pnext;
956 1.1 christos
957 1.1 christos if (ipe->ipn_pdnext != NULL)
958 1.1 christos *ipe->ipn_pdnext = ipe->ipn_dnext;
959 1.1 christos if (ipe->ipn_dnext != NULL)
960 1.1 christos ipe->ipn_dnext->ipn_pdnext = ipe->ipn_pdnext;
961 1.1 christos
962 1.1 christos ipo->ipo_head->deladdr(ipo->ipo_head, &ipe->ipn_addr, &ipe->ipn_mask);
963 1.1 christos
964 1.1 christos ipf_pool_node_deref(softp, ipe);
965 1.1 christos
966 1.1 christos return 0;
967 1.1 christos }
968 1.1 christos
969 1.1 christos
970 1.1 christos /* ------------------------------------------------------------------------ */
971 1.1 christos /* Function: ipf_pool_destroy */
972 1.1 christos /* Returns: int - 0 = success, else error */
973 1.1 christos /* Parameters: softc(I) - pointer to soft context main structure */
974 1.1 christos /* softp(I) - pointer to soft context pool information */
975 1.1 christos /* unit(I) - ipfilter device to which we are working on */
976 1.1 christos /* name(I) - name of the pool */
977 1.1 christos /* Locks: WRITE(ipf_poolrw) or WRITE(ipf_global) */
978 1.1 christos /* */
979 1.1 christos /* Search for a pool using paramters passed in and if it's not otherwise */
980 1.1 christos /* busy, free it. If it is busy, clear all of its nodes, mark it for being */
981 1.1 christos /* deleted and return an error saying it is busy. */
982 1.1 christos /* */
983 1.1 christos /* NOTE: Because this function is called out of ipfdetach() where ipf_poolrw*/
984 1.1 christos /* may not be initialised, we can't use an ASSERT to enforce the locking */
985 1.1 christos /* assertion that one of the two (ipf_poolrw,ipf_global) is held. */
986 1.1 christos /* ------------------------------------------------------------------------ */
987 1.1 christos static int
988 1.2 christos ipf_pool_destroy(ipf_main_softc_t *softc, ipf_pool_softc_t *softp, int unit,
989 1.2 christos char *name)
990 1.1 christos {
991 1.1 christos ip_pool_t *ipo;
992 1.1 christos
993 1.1 christos ipo = ipf_pool_exists(softp, unit, name);
994 1.1 christos if (ipo == NULL) {
995 1.1 christos IPFERROR(70009);
996 1.1 christos return ESRCH;
997 1.1 christos }
998 1.1 christos
999 1.1 christos if (ipo->ipo_ref != 1) {
1000 1.1 christos ipf_pool_clearnodes(softp, ipo);
1001 1.1 christos ipo->ipo_flags |= IPOOL_DELETE;
1002 1.1 christos return 0;
1003 1.1 christos }
1004 1.1 christos
1005 1.1 christos ipf_pool_free(softp, ipo);
1006 1.1 christos return 0;
1007 1.1 christos }
1008 1.1 christos
1009 1.1 christos
1010 1.1 christos /* ------------------------------------------------------------------------ */
1011 1.1 christos /* Function: ipf_pool_flush */
1012 1.1 christos /* Returns: int - number of pools deleted */
1013 1.1 christos /* Parameters: softc(I) - pointer to soft context main structure */
1014 1.1 christos /* arg(I) - pointer to local context to use */
1015 1.1 christos /* fp(I) - which pool(s) to flush */
1016 1.1 christos /* Locks: WRITE(ipf_poolrw) or WRITE(ipf_global) */
1017 1.1 christos /* */
1018 1.1 christos /* Free all pools associated with the device that matches the unit number */
1019 1.1 christos /* passed in with operation. */
1020 1.1 christos /* */
1021 1.1 christos /* NOTE: Because this function is called out of ipfdetach() where ipf_poolrw*/
1022 1.1 christos /* may not be initialised, we can't use an ASSERT to enforce the locking */
1023 1.1 christos /* assertion that one of the two (ipf_poolrw,ipf_global) is held. */
1024 1.1 christos /* ------------------------------------------------------------------------ */
1025 1.1 christos static size_t
1026 1.2 christos ipf_pool_flush(ipf_main_softc_t *softc, void *arg, iplookupflush_t *fp)
1027 1.1 christos {
1028 1.1 christos ipf_pool_softc_t *softp = arg;
1029 1.1 christos int i, num = 0, unit, err;
1030 1.1 christos ip_pool_t *p, *q;
1031 1.1 christos
1032 1.1 christos unit = fp->iplf_unit;
1033 1.1 christos for (i = -1; i <= IPL_LOGMAX; i++) {
1034 1.1 christos if (unit != IPLT_ALL && i != unit)
1035 1.1 christos continue;
1036 1.1 christos for (q = softp->ipf_pool_list[i + 1]; (p = q) != NULL; ) {
1037 1.1 christos q = p->ipo_next;
1038 1.1 christos err = ipf_pool_destroy(softc, softp, i, p->ipo_name);
1039 1.1 christos if (err == 0)
1040 1.1 christos num++;
1041 1.1 christos }
1042 1.1 christos }
1043 1.1 christos return num;
1044 1.1 christos }
1045 1.1 christos
1046 1.1 christos
1047 1.1 christos /* ------------------------------------------------------------------------ */
1048 1.1 christos /* Function: ipf_pool_free */
1049 1.1 christos /* Returns: void */
1050 1.1 christos /* Parameters: softp(I) - pointer to soft context pool information */
1051 1.1 christos /* ipo(I) - pointer to pool structure */
1052 1.1 christos /* Locks: WRITE(ipf_poolrw) or WRITE(ipf_global) */
1053 1.1 christos /* */
1054 1.1 christos /* Deletes the pool strucutre passed in from the list of pools and deletes */
1055 1.1 christos /* all of the address information stored in it, including any tree data */
1056 1.1 christos /* structures also allocated. */
1057 1.1 christos /* */
1058 1.1 christos /* NOTE: Because this function is called out of ipfdetach() where ipf_poolrw*/
1059 1.1 christos /* may not be initialised, we can't use an ASSERT to enforce the locking */
1060 1.1 christos /* assertion that one of the two (ipf_poolrw,ipf_global) is held. */
1061 1.1 christos /* ------------------------------------------------------------------------ */
1062 1.1 christos static void
1063 1.2 christos ipf_pool_free(ipf_pool_softc_t *softp, ip_pool_t *ipo)
1064 1.1 christos {
1065 1.1 christos
1066 1.1 christos ipf_pool_clearnodes(softp, ipo);
1067 1.1 christos
1068 1.1 christos if (ipo->ipo_next != NULL)
1069 1.1 christos ipo->ipo_next->ipo_pnext = ipo->ipo_pnext;
1070 1.1 christos *ipo->ipo_pnext = ipo->ipo_next;
1071 1.1 christos ipf_rx_freehead(ipo->ipo_head);
1072 1.1 christos KFREE(ipo);
1073 1.1 christos
1074 1.1 christos softp->ipf_pool_stats.ipls_pools--;
1075 1.1 christos }
1076 1.1 christos
1077 1.1 christos
1078 1.1 christos /* ------------------------------------------------------------------------ */
1079 1.1 christos /* Function: ipf_pool_clearnodes */
1080 1.1 christos /* Returns: void */
1081 1.1 christos /* Parameters: softp(I) - pointer to soft context pool information */
1082 1.1 christos /* ipo(I) - pointer to pool structure */
1083 1.1 christos /* Locks: WRITE(ipf_poolrw) or WRITE(ipf_global) */
1084 1.1 christos /* */
1085 1.1 christos /* Deletes all nodes stored in a pool structure. */
1086 1.1 christos /* ------------------------------------------------------------------------ */
1087 1.1 christos static void
1088 1.2 christos ipf_pool_clearnodes(ipf_pool_softc_t *softp, ip_pool_t *ipo)
1089 1.1 christos {
1090 1.1 christos ip_pool_node_t *n, **next;
1091 1.1 christos
1092 1.1 christos for (next = &ipo->ipo_list; (n = *next) != NULL; ) {
1093 1.1 christos ipo->ipo_head->deladdr(ipo->ipo_head, &n->ipn_addr,
1094 1.1 christos &n->ipn_mask);
1095 1.1 christos
1096 1.1 christos *n->ipn_pnext = n->ipn_next;
1097 1.1 christos if (n->ipn_next)
1098 1.1 christos n->ipn_next->ipn_pnext = n->ipn_pnext;
1099 1.1 christos
1100 1.1 christos if (n->ipn_pdnext != NULL) {
1101 1.1 christos *n->ipn_pdnext = n->ipn_dnext;
1102 1.1 christos if (n->ipn_dnext)
1103 1.1 christos n->ipn_dnext->ipn_pdnext = n->ipn_pdnext;
1104 1.1 christos }
1105 1.1 christos
1106 1.1 christos KFREE(n);
1107 1.1 christos
1108 1.1 christos softp->ipf_pool_stats.ipls_nodes--;
1109 1.1 christos }
1110 1.1 christos
1111 1.1 christos ipo->ipo_list = NULL;
1112 1.1 christos }
1113 1.1 christos
1114 1.1 christos
1115 1.1 christos /* ------------------------------------------------------------------------ */
1116 1.1 christos /* Function: ipf_pool_deref */
1117 1.1 christos /* Returns: void */
1118 1.1 christos /* Parameters: softc(I) - pointer to soft context main structure */
1119 1.1 christos /* arg(I) - pointer to local context to use */
1120 1.1 christos /* pool(I) - pointer to pool structure */
1121 1.1 christos /* Locks: WRITE(ipf_poolrw) */
1122 1.1 christos /* */
1123 1.1 christos /* Drop the number of known references to this pool structure by one and if */
1124 1.1 christos /* we arrive at zero known references, free it. */
1125 1.1 christos /* ------------------------------------------------------------------------ */
1126 1.1 christos static int
1127 1.2 christos ipf_pool_deref(ipf_main_softc_t *softc, void *arg, void *pool)
1128 1.1 christos {
1129 1.1 christos ip_pool_t *ipo = pool;
1130 1.1 christos
1131 1.1 christos ipo->ipo_ref--;
1132 1.1 christos
1133 1.1 christos if (ipo->ipo_ref == 0)
1134 1.1 christos ipf_pool_free(arg, ipo);
1135 1.1 christos
1136 1.1 christos else if ((ipo->ipo_ref == 1) && (ipo->ipo_flags & IPOOL_DELETE))
1137 1.1 christos ipf_pool_destroy(softc, arg, ipo->ipo_unit, ipo->ipo_name);
1138 1.1 christos
1139 1.1 christos return 0;
1140 1.1 christos }
1141 1.1 christos
1142 1.1 christos
1143 1.1 christos /* ------------------------------------------------------------------------ */
1144 1.1 christos /* Function: ipf_pool_node_deref */
1145 1.1 christos /* Returns: void */
1146 1.1 christos /* Parameters: softp(I) - pointer to soft context pool information */
1147 1.1 christos /* ipn(I) - pointer to pool structure */
1148 1.1 christos /* Locks: WRITE(ipf_poolrw) */
1149 1.1 christos /* */
1150 1.1 christos /* Drop a reference to the pool node passed in and if we're the last, free */
1151 1.1 christos /* it all up and adjust the stats accordingly. */
1152 1.1 christos /* ------------------------------------------------------------------------ */
1153 1.1 christos static void
1154 1.2 christos ipf_pool_node_deref(ipf_pool_softc_t *softp, ip_pool_node_t *ipn)
1155 1.1 christos {
1156 1.1 christos
1157 1.1 christos ipn->ipn_ref--;
1158 1.1 christos
1159 1.1 christos if (ipn->ipn_ref == 0) {
1160 1.1 christos KFREE(ipn);
1161 1.1 christos softp->ipf_pool_stats.ipls_nodes--;
1162 1.1 christos }
1163 1.1 christos }
1164 1.1 christos
1165 1.1 christos
1166 1.1 christos /* ------------------------------------------------------------------------ */
1167 1.1 christos /* Function: ipf_pool_iter_next */
1168 1.1 christos /* Returns: void */
1169 1.1 christos /* Parameters: softc(I) - pointer to soft context main structure */
1170 1.1 christos /* arg(I) - pointer to local context to use */
1171 1.1 christos /* token(I) - pointer to pool structure */
1172 1.1 christos /* ilp(IO) - pointer to pool iterating structure */
1173 1.1 christos /* */
1174 1.1 christos /* ------------------------------------------------------------------------ */
1175 1.1 christos static int
1176 1.2 christos ipf_pool_iter_next(ipf_main_softc_t *softc, void *arg, ipftoken_t *token,
1177 1.2 christos ipflookupiter_t *ilp)
1178 1.1 christos {
1179 1.1 christos ipf_pool_softc_t *softp = arg;
1180 1.1 christos ip_pool_node_t *node, zn, *nextnode;
1181 1.1 christos ip_pool_t *ipo, zp, *nextipo;
1182 1.1 christos void *pnext;
1183 1.1 christos int err;
1184 1.1 christos
1185 1.1 christos err = 0;
1186 1.1 christos node = NULL;
1187 1.1 christos nextnode = NULL;
1188 1.1 christos ipo = NULL;
1189 1.1 christos nextipo = NULL;
1190 1.1 christos
1191 1.1 christos READ_ENTER(&softc->ipf_poolrw);
1192 1.1 christos
1193 1.1 christos switch (ilp->ili_otype)
1194 1.1 christos {
1195 1.1 christos case IPFLOOKUPITER_LIST :
1196 1.1 christos ipo = token->ipt_data;
1197 1.1 christos if (ipo == NULL) {
1198 1.1 christos nextipo = softp->ipf_pool_list[(int)ilp->ili_unit + 1];
1199 1.1 christos } else {
1200 1.1 christos nextipo = ipo->ipo_next;
1201 1.1 christos }
1202 1.1 christos
1203 1.1 christos if (nextipo != NULL) {
1204 1.1 christos ATOMIC_INC32(nextipo->ipo_ref);
1205 1.1 christos token->ipt_data = nextipo;
1206 1.1 christos } else {
1207 1.1 christos bzero((char *)&zp, sizeof(zp));
1208 1.1 christos nextipo = &zp;
1209 1.1 christos token->ipt_data = NULL;
1210 1.1 christos }
1211 1.1 christos pnext = nextipo->ipo_next;
1212 1.1 christos break;
1213 1.1 christos
1214 1.1 christos case IPFLOOKUPITER_NODE :
1215 1.1 christos node = token->ipt_data;
1216 1.1 christos if (node == NULL) {
1217 1.1 christos ipo = ipf_pool_exists(arg, ilp->ili_unit,
1218 1.1 christos ilp->ili_name);
1219 1.1 christos if (ipo == NULL) {
1220 1.1 christos IPFERROR(70010);
1221 1.1 christos err = ESRCH;
1222 1.1 christos } else {
1223 1.1 christos nextnode = ipo->ipo_list;
1224 1.1 christos ipo = NULL;
1225 1.1 christos }
1226 1.1 christos } else {
1227 1.1 christos nextnode = node->ipn_next;
1228 1.1 christos }
1229 1.1 christos
1230 1.1 christos if (nextnode != NULL) {
1231 1.1 christos ATOMIC_INC32(nextnode->ipn_ref);
1232 1.1 christos token->ipt_data = nextnode;
1233 1.1 christos } else {
1234 1.1 christos bzero((char *)&zn, sizeof(zn));
1235 1.1 christos nextnode = &zn;
1236 1.1 christos token->ipt_data = NULL;
1237 1.1 christos }
1238 1.1 christos pnext = nextnode->ipn_next;
1239 1.1 christos break;
1240 1.1 christos
1241 1.1 christos default :
1242 1.1 christos IPFERROR(70011);
1243 1.1 christos pnext = NULL;
1244 1.1 christos err = EINVAL;
1245 1.1 christos break;
1246 1.1 christos }
1247 1.1 christos
1248 1.1 christos RWLOCK_EXIT(&softc->ipf_poolrw);
1249 1.1 christos if (err != 0)
1250 1.1 christos return err;
1251 1.1 christos
1252 1.1 christos switch (ilp->ili_otype)
1253 1.1 christos {
1254 1.1 christos case IPFLOOKUPITER_LIST :
1255 1.1 christos err = COPYOUT(nextipo, ilp->ili_data, sizeof(*nextipo));
1256 1.1 christos if (err != 0) {
1257 1.1 christos IPFERROR(70012);
1258 1.1 christos err = EFAULT;
1259 1.1 christos }
1260 1.1 christos if (ipo != NULL) {
1261 1.1 christos WRITE_ENTER(&softc->ipf_poolrw);
1262 1.1 christos ipf_pool_deref(softc, softp, ipo);
1263 1.1 christos RWLOCK_EXIT(&softc->ipf_poolrw);
1264 1.1 christos }
1265 1.1 christos break;
1266 1.1 christos
1267 1.1 christos case IPFLOOKUPITER_NODE :
1268 1.1 christos err = COPYOUT(nextnode, ilp->ili_data, sizeof(*nextnode));
1269 1.1 christos if (err != 0) {
1270 1.1 christos IPFERROR(70013);
1271 1.1 christos err = EFAULT;
1272 1.1 christos }
1273 1.1 christos if (node != NULL) {
1274 1.1 christos WRITE_ENTER(&softc->ipf_poolrw);
1275 1.1 christos ipf_pool_node_deref(softp, node);
1276 1.1 christos RWLOCK_EXIT(&softc->ipf_poolrw);
1277 1.1 christos }
1278 1.1 christos break;
1279 1.1 christos }
1280 1.1 christos if (pnext == NULL)
1281 1.1 christos ipf_token_mark_complete(token);
1282 1.1 christos
1283 1.1 christos return err;
1284 1.1 christos }
1285 1.1 christos
1286 1.1 christos
1287 1.1 christos /* ------------------------------------------------------------------------ */
1288 1.1 christos /* Function: ipf_pool_iterderef */
1289 1.1 christos /* Returns: void */
1290 1.1 christos /* Parameters: softc(I) - pointer to soft context main structure */
1291 1.1 christos /* arg(I) - pointer to local context to use */
1292 1.1 christos /* unit(I) - ipfilter device to which we are working on */
1293 1.1 christos /* Locks: WRITE(ipf_poolrw) */
1294 1.1 christos /* */
1295 1.1 christos /* ------------------------------------------------------------------------ */
1296 1.1 christos static int
1297 1.2 christos ipf_pool_iter_deref(ipf_main_softc_t *softc, void *arg, int otype, int unit,
1298 1.2 christos void *data)
1299 1.1 christos {
1300 1.1 christos ipf_pool_softc_t *softp = arg;
1301 1.1 christos
1302 1.1 christos if (data == NULL)
1303 1.1 christos return EINVAL;
1304 1.1 christos
1305 1.1 christos if (unit < 0 || unit > IPL_LOGMAX)
1306 1.1 christos return EINVAL;
1307 1.1 christos
1308 1.1 christos switch (otype)
1309 1.1 christos {
1310 1.1 christos case IPFLOOKUPITER_LIST :
1311 1.1 christos ipf_pool_deref(softc, softp, (ip_pool_t *)data);
1312 1.1 christos break;
1313 1.1 christos
1314 1.1 christos case IPFLOOKUPITER_NODE :
1315 1.1 christos ipf_pool_node_deref(softp, (ip_pool_node_t *)data);
1316 1.1 christos break;
1317 1.1 christos default :
1318 1.1 christos break;
1319 1.1 christos }
1320 1.1 christos
1321 1.1 christos return 0;
1322 1.1 christos }
1323 1.1 christos
1324 1.1 christos
1325 1.1 christos /* ------------------------------------------------------------------------ */
1326 1.1 christos /* Function: ipf_pool_expire */
1327 1.1 christos /* Returns: Nil */
1328 1.1 christos /* Parameters: softc(I) - pointer to soft context main structure */
1329 1.1 christos /* arg(I) - pointer to local context to use */
1330 1.1 christos /* */
1331 1.1 christos /* At present this function exists just to support temporary addition of */
1332 1.1 christos /* nodes to the address pool. */
1333 1.1 christos /* ------------------------------------------------------------------------ */
1334 1.1 christos static void
1335 1.2 christos ipf_pool_expire(ipf_main_softc_t *softc, void *arg)
1336 1.1 christos {
1337 1.1 christos ipf_pool_softc_t *softp = arg;
1338 1.1 christos ip_pool_node_t *n;
1339 1.1 christos
1340 1.1 christos while ((n = softp->ipf_node_explist) != NULL) {
1341 1.1 christos /*
1342 1.1 christos * Because the list is kept sorted on insertion, the fist
1343 1.1 christos * one that dies in the future means no more work to do.
1344 1.1 christos */
1345 1.1 christos if (n->ipn_die > softc->ipf_ticks)
1346 1.1 christos break;
1347 1.1 christos ipf_pool_remove_node(softp, n->ipn_owner, n);
1348 1.1 christos }
1349 1.1 christos }
1350 1.1 christos
1351 1.1 christos
1352 1.1 christos
1353 1.1 christos
1354 1.1 christos #ifndef _KERNEL
1355 1.1 christos void
1356 1.1 christos ipf_pool_dump(softc, arg)
1357 1.1 christos ipf_main_softc_t *softc;
1358 1.1 christos void *arg;
1359 1.1 christos {
1360 1.1 christos ipf_pool_softc_t *softp = arg;
1361 1.1 christos ip_pool_t *ipl;
1362 1.1 christos int i;
1363 1.1 christos
1364 1.1 christos printf("List of configured pools\n");
1365 1.1 christos for (i = 0; i <= LOOKUP_POOL_MAX; i++)
1366 1.1 christos for (ipl = softp->ipf_pool_list[i]; ipl != NULL;
1367 1.1 christos ipl = ipl->ipo_next)
1368 1.1 christos printpool(ipl, bcopywrap, NULL, opts, NULL);
1369 1.1 christos }
1370 1.1 christos #endif
1371