ip_lookup.c revision 1.2 1 1.2 christos /* $NetBSD: ip_lookup.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) 2010 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/param.h>
18 1.2 christos #if defined(__NetBSD__)
19 1.2 christos # if (NetBSD >= 199905) && !defined(IPFILTER_LKM) && defined(_KERNEL)
20 1.2 christos # include "opt_ipfilter.h"
21 1.2 christos # endif
22 1.2 christos #endif
23 1.1 christos #include <sys/errno.h>
24 1.1 christos #include <sys/types.h>
25 1.1 christos #include <sys/time.h>
26 1.1 christos #include <sys/file.h>
27 1.1 christos #if __FreeBSD_version >= 220000 && defined(_KERNEL)
28 1.1 christos # include <sys/fcntl.h>
29 1.1 christos # include <sys/filio.h>
30 1.1 christos #else
31 1.1 christos # include <sys/ioctl.h>
32 1.1 christos #endif
33 1.1 christos #if !defined(_KERNEL)
34 1.1 christos # include <stdio.h>
35 1.1 christos # include <string.h>
36 1.1 christos # include <stdlib.h>
37 1.1 christos # define _KERNEL
38 1.1 christos # ifdef __OpenBSD__
39 1.1 christos struct file;
40 1.1 christos # endif
41 1.1 christos # include <sys/uio.h>
42 1.1 christos # undef _KERNEL
43 1.1 christos #endif
44 1.1 christos #include <sys/socket.h>
45 1.1 christos #include <net/if.h>
46 1.1 christos #if defined(__FreeBSD__)
47 1.1 christos # include <sys/cdefs.h>
48 1.1 christos # include <sys/proc.h>
49 1.1 christos #endif
50 1.1 christos #if defined(_KERNEL)
51 1.1 christos # include <sys/systm.h>
52 1.1 christos # if !defined(__SVR4) && !defined(__svr4__)
53 1.1 christos # include <sys/mbuf.h>
54 1.1 christos # endif
55 1.1 christos #else
56 1.1 christos # include "ipf.h"
57 1.1 christos #endif
58 1.1 christos #include <netinet/in.h>
59 1.1 christos
60 1.1 christos #include "netinet/ip_compat.h"
61 1.1 christos #include "netinet/ip_fil.h"
62 1.1 christos #include "netinet/ip_lookup.h"
63 1.1 christos #include "netinet/ip_pool.h"
64 1.1 christos #include "netinet/ip_htable.h"
65 1.1 christos #include "netinet/ip_dstlist.h"
66 1.1 christos /* END OF INCLUDES */
67 1.1 christos
68 1.1 christos #if !defined(lint)
69 1.2 christos #if defined(__NetBSD__)
70 1.2 christos #include <sys/cdefs.h>
71 1.2 christos __KERNEL_RCSID(0, "$NetBSD: ip_lookup.c,v 1.2 2012/03/23 20:39:50 christos Exp $");
72 1.2 christos #else
73 1.2 christos static const char rcsid[] = "@(#)Id: ip_lookup.c,v 2.67.2.5 2012/01/29 05:30:36 darrenr Exp";
74 1.2 christos #endif
75 1.1 christos #endif
76 1.1 christos
77 1.1 christos /*
78 1.1 christos * In this file, ip_pool.c, ip_htable.c and ip_dstlist.c, you will find the
79 1.1 christos * range for unit is [-1,IPL_LOGMAX]. The -1 is considered to be a valid number
80 1.1 christos * and represents a "wildcard" or "all" units (IPL_LOGALL). The reason for not
81 1.1 christos * starting the numbering at 0 is because the numbers [0,IPL_LOGMAX] correspond
82 1.1 christos * to the minor device number for their respective device. Thus where there is
83 1.1 christos * array indexing on the unit, +1 is used to map [-1.IPL_LOGMAX] to
84 1.1 christos * [0.POOL_LOOKUP_MAX].
85 1.1 christos */
86 1.2 christos static int ipf_lookup_addnode(ipf_main_softc_t *, void *, int);
87 1.2 christos static int ipf_lookup_delnode(ipf_main_softc_t *, void *, int);
88 1.2 christos static int ipf_lookup_addtable(ipf_main_softc_t *, void *);
89 1.2 christos static int ipf_lookup_deltable(ipf_main_softc_t *, void *);
90 1.2 christos static int ipf_lookup_stats(ipf_main_softc_t *, void *);
91 1.2 christos static int ipf_lookup_flush(ipf_main_softc_t *, void *);
92 1.2 christos static int ipf_lookup_iterate(ipf_main_softc_t *, void *, int, void *);
93 1.2 christos static int ipf_lookup_deltok(ipf_main_softc_t *, void *, int, void *);
94 1.1 christos
95 1.1 christos #define MAX_BACKENDS 3
96 1.1 christos static ipf_lookup_t *backends[MAX_BACKENDS] = {
97 1.1 christos &ipf_pool_backend,
98 1.1 christos &ipf_htable_backend,
99 1.1 christos &ipf_dstlist_backend
100 1.1 christos };
101 1.1 christos
102 1.1 christos
103 1.1 christos typedef struct ipf_lookup_softc_s {
104 1.1 christos void *ipf_back[MAX_BACKENDS];
105 1.1 christos } ipf_lookup_softc_t;
106 1.1 christos
107 1.1 christos
108 1.1 christos /* ------------------------------------------------------------------------ */
109 1.1 christos /* Function: ipf_lookup_init */
110 1.1 christos /* Returns: int - 0 = success, else error */
111 1.1 christos /* Parameters: softc(I) - pointer to soft context main structure */
112 1.1 christos /* */
113 1.1 christos /* Initialise all of the subcomponents of the lookup infrstructure. */
114 1.1 christos /* ------------------------------------------------------------------------ */
115 1.1 christos void *
116 1.2 christos ipf_lookup_soft_create(ipf_main_softc_t *softc)
117 1.1 christos {
118 1.1 christos ipf_lookup_softc_t *softl;
119 1.1 christos ipf_lookup_t **l;
120 1.1 christos int i;
121 1.1 christos
122 1.1 christos KMALLOC(softl, ipf_lookup_softc_t *);
123 1.1 christos if (softl == NULL)
124 1.1 christos return NULL;
125 1.1 christos
126 1.1 christos bzero((char *)softl, sizeof(*softl));
127 1.1 christos
128 1.1 christos for (i = 0, l = backends; i < MAX_BACKENDS; i++, l++) {
129 1.1 christos softl->ipf_back[i] = (*(*l)->ipfl_create)(softc);
130 1.1 christos if (softl->ipf_back[i] == NULL) {
131 1.1 christos ipf_lookup_soft_destroy(softc, softl);
132 1.1 christos return NULL;
133 1.1 christos }
134 1.1 christos }
135 1.1 christos
136 1.1 christos return softl;
137 1.1 christos }
138 1.1 christos
139 1.1 christos
140 1.1 christos /* ------------------------------------------------------------------------ */
141 1.1 christos /* Function: ipf_lookup_soft_init */
142 1.1 christos /* Returns: int - 0 = success, else error */
143 1.1 christos /* Parameters: softc(I) - pointer to soft context main structure */
144 1.1 christos /* arg(I) - pointer to local context to use */
145 1.1 christos /* */
146 1.1 christos /* Initialise all of the subcomponents of the lookup infrstructure. */
147 1.1 christos /* ------------------------------------------------------------------------ */
148 1.1 christos int
149 1.2 christos ipf_lookup_soft_init(ipf_main_softc_t *softc, void *arg)
150 1.1 christos {
151 1.1 christos ipf_lookup_softc_t *softl = (ipf_lookup_softc_t *)arg;
152 1.1 christos int err = 0;
153 1.1 christos int i;
154 1.1 christos
155 1.1 christos for (i = 0; i < MAX_BACKENDS; i++) {
156 1.1 christos err = (*backends[i]->ipfl_init)(softc, softl->ipf_back[i]);
157 1.1 christos if (err != 0)
158 1.1 christos break;
159 1.1 christos }
160 1.1 christos
161 1.1 christos return err;
162 1.1 christos }
163 1.1 christos
164 1.1 christos
165 1.1 christos /* ------------------------------------------------------------------------ */
166 1.1 christos /* Function: ipf_lookup_soft_fini */
167 1.1 christos /* Returns: int - 0 = success, else error */
168 1.1 christos /* Parameters: softc(I) - pointer to soft context main structure */
169 1.1 christos /* arg(I) - pointer to local context to use */
170 1.1 christos /* */
171 1.1 christos /* Call the fini function in each backend to cleanup all allocated data. */
172 1.1 christos /* ------------------------------------------------------------------------ */
173 1.1 christos int
174 1.2 christos ipf_lookup_soft_fini(ipf_main_softc_t *softc, void *arg)
175 1.1 christos {
176 1.1 christos ipf_lookup_softc_t *softl = (ipf_lookup_softc_t *)arg;
177 1.1 christos int i;
178 1.1 christos
179 1.1 christos for (i = 0; i < MAX_BACKENDS; i++) {
180 1.1 christos if (softl->ipf_back[i] != NULL)
181 1.1 christos (*backends[i]->ipfl_fini)(softc,
182 1.1 christos softl->ipf_back[i]);
183 1.1 christos }
184 1.1 christos
185 1.1 christos return 0;
186 1.1 christos }
187 1.1 christos
188 1.1 christos
189 1.1 christos /* ------------------------------------------------------------------------ */
190 1.1 christos /* Function: ipf_lookup_expire */
191 1.1 christos /* Returns: Nil */
192 1.1 christos /* Parameters: softc(I) - pointer to soft context main structure */
193 1.1 christos /* */
194 1.1 christos /* Step through each of the backends and call their expire functions, */
195 1.1 christos /* allowing them to delete any lifetime limited data. */
196 1.1 christos /* ------------------------------------------------------------------------ */
197 1.1 christos void
198 1.2 christos ipf_lookup_expire(ipf_main_softc_t *softc)
199 1.1 christos {
200 1.1 christos ipf_lookup_softc_t *softl = softc->ipf_lookup_soft;
201 1.1 christos int i;
202 1.1 christos
203 1.1 christos WRITE_ENTER(&softc->ipf_poolrw);
204 1.1 christos for (i = 0; i < MAX_BACKENDS; i++)
205 1.1 christos (*backends[i]->ipfl_expire)(softc, softl->ipf_back[i]);
206 1.1 christos RWLOCK_EXIT(&softc->ipf_poolrw);
207 1.1 christos }
208 1.1 christos
209 1.1 christos
210 1.1 christos /* ------------------------------------------------------------------------ */
211 1.1 christos /* Function: ipf_lookup_softc_destroy */
212 1.1 christos /* Returns: int - 0 = success, else error */
213 1.1 christos /* Parameters: softc(I) - pointer to soft context main structure */
214 1.1 christos /* arg(I) - pointer to local context to use */
215 1.1 christos /* */
216 1.1 christos /* Free up all pool related memory that has been allocated whilst IPFilter */
217 1.1 christos /* has been running. Also, do any other deinitialisation required such */
218 1.1 christos /* ipf_lookup_init() can be called again, safely. */
219 1.1 christos /* ------------------------------------------------------------------------ */
220 1.1 christos void
221 1.2 christos ipf_lookup_soft_destroy(ipf_main_softc_t *softc, void *arg)
222 1.1 christos {
223 1.1 christos ipf_lookup_softc_t *softl = (ipf_lookup_softc_t *)arg;
224 1.1 christos int i;
225 1.1 christos
226 1.1 christos for (i = 0; i < MAX_BACKENDS; i++) {
227 1.1 christos if (softl->ipf_back[i] != NULL)
228 1.1 christos (*backends[i]->ipfl_destroy)(softc,
229 1.1 christos softl->ipf_back[i]);
230 1.1 christos }
231 1.1 christos
232 1.1 christos KFREE(softl);
233 1.1 christos }
234 1.1 christos
235 1.1 christos
236 1.1 christos /* ------------------------------------------------------------------------ */
237 1.1 christos /* Function: ipf_lookup_ioctl */
238 1.1 christos /* Returns: int - 0 = success, else error */
239 1.1 christos /* Parameters: softc(I) - pointer to soft context main structure */
240 1.1 christos /* arg(I) - pointer to local context to use */
241 1.1 christos /* data(IO) - pointer to ioctl data to be copied to/from user */
242 1.1 christos /* space. */
243 1.1 christos /* cmd(I) - ioctl command number */
244 1.1 christos /* mode(I) - file mode bits used with open */
245 1.1 christos /* uid(I) - uid of process doing ioctl */
246 1.1 christos /* ctx(I) - pointer that represents context for uid */
247 1.1 christos /* */
248 1.1 christos /* Handle ioctl commands sent to the ioctl device. For the most part, this */
249 1.1 christos /* involves just calling another function to handle the specifics of each */
250 1.1 christos /* command. */
251 1.1 christos /* ------------------------------------------------------------------------ */
252 1.1 christos int
253 1.2 christos ipf_lookup_ioctl(ipf_main_softc_t *softc, void *data, ioctlcmd_t cmd,
254 1.2 christos int mode, int uid, void *ctx)
255 1.1 christos {
256 1.1 christos int err;
257 1.1 christos SPL_INT(s);
258 1.1 christos
259 1.1 christos mode = mode; /* LINT */
260 1.1 christos
261 1.1 christos SPL_NET(s);
262 1.1 christos
263 1.1 christos switch (cmd)
264 1.1 christos {
265 1.1 christos case SIOCLOOKUPADDNODE :
266 1.1 christos case SIOCLOOKUPADDNODEW :
267 1.1 christos WRITE_ENTER(&softc->ipf_poolrw);
268 1.1 christos err = ipf_lookup_addnode(softc, data, uid);
269 1.1 christos RWLOCK_EXIT(&softc->ipf_poolrw);
270 1.1 christos break;
271 1.1 christos
272 1.1 christos case SIOCLOOKUPDELNODE :
273 1.1 christos case SIOCLOOKUPDELNODEW :
274 1.1 christos WRITE_ENTER(&softc->ipf_poolrw);
275 1.1 christos err = ipf_lookup_delnode(softc, data, uid);
276 1.1 christos RWLOCK_EXIT(&softc->ipf_poolrw);
277 1.1 christos break;
278 1.1 christos
279 1.1 christos case SIOCLOOKUPADDTABLE :
280 1.1 christos WRITE_ENTER(&softc->ipf_poolrw);
281 1.1 christos err = ipf_lookup_addtable(softc, data);
282 1.1 christos RWLOCK_EXIT(&softc->ipf_poolrw);
283 1.1 christos break;
284 1.1 christos
285 1.1 christos case SIOCLOOKUPDELTABLE :
286 1.1 christos WRITE_ENTER(&softc->ipf_poolrw);
287 1.1 christos err = ipf_lookup_deltable(softc, data);
288 1.1 christos RWLOCK_EXIT(&softc->ipf_poolrw);
289 1.1 christos break;
290 1.1 christos
291 1.1 christos case SIOCLOOKUPSTAT :
292 1.1 christos case SIOCLOOKUPSTATW :
293 1.1 christos WRITE_ENTER(&softc->ipf_poolrw);
294 1.1 christos err = ipf_lookup_stats(softc, data);
295 1.1 christos RWLOCK_EXIT(&softc->ipf_poolrw);
296 1.1 christos break;
297 1.1 christos
298 1.1 christos case SIOCLOOKUPFLUSH :
299 1.1 christos WRITE_ENTER(&softc->ipf_poolrw);
300 1.1 christos err = ipf_lookup_flush(softc, data);
301 1.1 christos RWLOCK_EXIT(&softc->ipf_poolrw);
302 1.1 christos break;
303 1.1 christos
304 1.1 christos case SIOCLOOKUPITER :
305 1.1 christos err = ipf_lookup_iterate(softc, data, uid, ctx);
306 1.1 christos break;
307 1.1 christos
308 1.1 christos case SIOCIPFDELTOK :
309 1.1 christos err = ipf_lookup_deltok(softc, data, uid, ctx);
310 1.1 christos break;
311 1.1 christos
312 1.1 christos default :
313 1.1 christos IPFERROR(50001);
314 1.1 christos err = EINVAL;
315 1.1 christos break;
316 1.1 christos }
317 1.1 christos SPL_X(s);
318 1.1 christos return err;
319 1.1 christos }
320 1.1 christos
321 1.1 christos
322 1.1 christos /* ------------------------------------------------------------------------ */
323 1.1 christos /* Function: ipf_lookup_addnode */
324 1.1 christos /* Returns: int - 0 = success, else error */
325 1.1 christos /* Parameters: softc(I) - pointer to soft context main structure */
326 1.1 christos /* data(I) - pointer to data from ioctl call */
327 1.1 christos /* */
328 1.1 christos /* Add a new data node to a lookup structure. First, check to see if the */
329 1.1 christos /* parent structure refered to by name exists and if it does, then go on to */
330 1.1 christos /* add a node to it. */
331 1.1 christos /* ------------------------------------------------------------------------ */
332 1.1 christos static int
333 1.2 christos ipf_lookup_addnode(ipf_main_softc_t *softc, void *data, int uid)
334 1.1 christos {
335 1.1 christos ipf_lookup_softc_t *softl = softc->ipf_lookup_soft;
336 1.1 christos iplookupop_t op;
337 1.1 christos ipf_lookup_t **l;
338 1.1 christos int err;
339 1.1 christos int i;
340 1.1 christos
341 1.1 christos err = BCOPYIN(data, &op, sizeof(op));
342 1.1 christos if (err != 0) {
343 1.1 christos IPFERROR(50002);
344 1.1 christos return EFAULT;
345 1.1 christos }
346 1.1 christos
347 1.1 christos if ((op.iplo_unit < 0 || op.iplo_unit > IPL_LOGMAX) &&
348 1.1 christos (op.iplo_unit != IPLT_ALL)) {
349 1.1 christos IPFERROR(50003);
350 1.1 christos return EINVAL;
351 1.1 christos }
352 1.1 christos
353 1.1 christos op.iplo_name[sizeof(op.iplo_name) - 1] = '\0';
354 1.1 christos
355 1.1 christos for (i = 0, l = backends; i < MAX_BACKENDS; i++, l++) {
356 1.1 christos if (op.iplo_type == (*l)->ipfl_type) {
357 1.1 christos err = (*(*l)->ipfl_node_add)(softc,
358 1.1 christos softl->ipf_back[i],
359 1.1 christos &op, uid);
360 1.1 christos break;
361 1.1 christos }
362 1.1 christos }
363 1.1 christos
364 1.1 christos if (i == MAX_BACKENDS) {
365 1.1 christos IPFERROR(50012);
366 1.1 christos err = EINVAL;
367 1.1 christos }
368 1.1 christos
369 1.1 christos return err;
370 1.1 christos }
371 1.1 christos
372 1.1 christos
373 1.1 christos /* ------------------------------------------------------------------------ */
374 1.1 christos /* Function: ipf_lookup_delnode */
375 1.1 christos /* Returns: int - 0 = success, else error */
376 1.1 christos /* Parameters: softc(I) - pointer to soft context main structure */
377 1.1 christos /* data(I) - pointer to data from ioctl call */
378 1.1 christos /* */
379 1.1 christos /* Delete a node from a lookup table by first looking for the table it is */
380 1.1 christos /* in and then deleting the entry that gets found. */
381 1.1 christos /* ------------------------------------------------------------------------ */
382 1.1 christos static int
383 1.2 christos ipf_lookup_delnode(ipf_main_softc_t *softc, void *data, int uid)
384 1.1 christos {
385 1.1 christos ipf_lookup_softc_t *softl = softc->ipf_lookup_soft;
386 1.1 christos iplookupop_t op;
387 1.1 christos ipf_lookup_t **l;
388 1.1 christos int err;
389 1.1 christos int i;
390 1.1 christos
391 1.1 christos err = BCOPYIN(data, &op, sizeof(op));
392 1.1 christos if (err != 0) {
393 1.1 christos IPFERROR(50042);
394 1.1 christos return EFAULT;
395 1.1 christos }
396 1.1 christos
397 1.1 christos if ((op.iplo_unit < 0 || op.iplo_unit > IPL_LOGMAX) &&
398 1.1 christos (op.iplo_unit != IPLT_ALL)) {
399 1.1 christos IPFERROR(50013);
400 1.1 christos return EINVAL;
401 1.1 christos }
402 1.1 christos
403 1.1 christos op.iplo_name[sizeof(op.iplo_name) - 1] = '\0';
404 1.1 christos
405 1.1 christos for (i = 0, l = backends; i < MAX_BACKENDS; i++, l++) {
406 1.1 christos if (op.iplo_type == (*l)->ipfl_type) {
407 1.1 christos err = (*(*l)->ipfl_node_del)(softc, softl->ipf_back[i],
408 1.1 christos &op, uid);
409 1.1 christos break;
410 1.1 christos }
411 1.1 christos }
412 1.1 christos
413 1.1 christos if (i == MAX_BACKENDS) {
414 1.1 christos IPFERROR(50021);
415 1.1 christos err = EINVAL;
416 1.1 christos }
417 1.1 christos return err;
418 1.1 christos }
419 1.1 christos
420 1.1 christos
421 1.1 christos /* ------------------------------------------------------------------------ */
422 1.1 christos /* Function: ipf_lookup_addtable */
423 1.1 christos /* Returns: int - 0 = success, else error */
424 1.1 christos /* Parameters: softc(I) - pointer to soft context main structure */
425 1.1 christos /* data(I) - pointer to data from ioctl call */
426 1.1 christos /* */
427 1.1 christos /* Create a new lookup table, if one doesn't already exist using the name */
428 1.1 christos /* for this one. */
429 1.1 christos /* ------------------------------------------------------------------------ */
430 1.1 christos static int
431 1.2 christos ipf_lookup_addtable(ipf_main_softc_t *softc, void *data)
432 1.1 christos {
433 1.1 christos ipf_lookup_softc_t *softl = softc->ipf_lookup_soft;
434 1.1 christos iplookupop_t op;
435 1.1 christos ipf_lookup_t **l;
436 1.1 christos int err, i;
437 1.1 christos
438 1.1 christos err = BCOPYIN(data, &op, sizeof(op));
439 1.1 christos if (err != 0) {
440 1.1 christos IPFERROR(50022);
441 1.1 christos return EFAULT;
442 1.1 christos }
443 1.1 christos
444 1.1 christos if ((op.iplo_unit < 0 || op.iplo_unit > IPL_LOGMAX) &&
445 1.1 christos (op.iplo_unit != IPLT_ALL)) {
446 1.1 christos IPFERROR(50023);
447 1.1 christos return EINVAL;
448 1.1 christos }
449 1.1 christos
450 1.1 christos op.iplo_name[sizeof(op.iplo_name) - 1] = '\0';
451 1.1 christos
452 1.1 christos for (i = 0, l = backends; i < MAX_BACKENDS; i++, l++) {
453 1.1 christos if (op.iplo_type == (*l)->ipfl_type) {
454 1.1 christos err = (*(*l)->ipfl_table_add)(softc,
455 1.1 christos softl->ipf_back[i],
456 1.1 christos &op);
457 1.1 christos break;
458 1.1 christos }
459 1.1 christos }
460 1.1 christos
461 1.1 christos if (i == MAX_BACKENDS) {
462 1.1 christos IPFERROR(50026);
463 1.1 christos err = EINVAL;
464 1.1 christos }
465 1.1 christos
466 1.1 christos /*
467 1.1 christos * For anonymous pools, copy back the operation struct because in the
468 1.1 christos * case of success it will contain the new table's name.
469 1.1 christos */
470 1.1 christos if ((err == 0) && ((op.iplo_arg & LOOKUP_ANON) != 0)) {
471 1.1 christos err = BCOPYOUT(&op, data, sizeof(op));
472 1.1 christos if (err != 0) {
473 1.1 christos IPFERROR(50027);
474 1.1 christos err = EFAULT;
475 1.1 christos }
476 1.1 christos }
477 1.1 christos
478 1.1 christos return err;
479 1.1 christos }
480 1.1 christos
481 1.1 christos
482 1.1 christos /* ------------------------------------------------------------------------ */
483 1.1 christos /* Function: ipf_lookup_deltable */
484 1.1 christos /* Returns: int - 0 = success, else error */
485 1.1 christos /* Parameters: softc(I) - pointer to soft context main structure */
486 1.1 christos /* data(I) - pointer to data from ioctl call */
487 1.1 christos /* */
488 1.1 christos /* Decodes ioctl request to remove a particular hash table or pool and */
489 1.1 christos /* calls the relevant function to do the cleanup. */
490 1.1 christos /* ------------------------------------------------------------------------ */
491 1.1 christos static int
492 1.2 christos ipf_lookup_deltable(ipf_main_softc_t *softc, void *data)
493 1.1 christos {
494 1.1 christos ipf_lookup_softc_t *softl = softc->ipf_lookup_soft;
495 1.1 christos iplookupop_t op;
496 1.1 christos ipf_lookup_t **l;
497 1.1 christos int err, i;
498 1.1 christos
499 1.1 christos err = BCOPYIN(data, &op, sizeof(op));
500 1.1 christos if (err != 0) {
501 1.1 christos IPFERROR(50028);
502 1.1 christos return EFAULT;
503 1.1 christos }
504 1.1 christos
505 1.1 christos if ((op.iplo_unit < 0 || op.iplo_unit > IPL_LOGMAX) &&
506 1.1 christos (op.iplo_unit != IPLT_ALL)) {
507 1.1 christos IPFERROR(50029);
508 1.1 christos return EINVAL;
509 1.1 christos }
510 1.1 christos
511 1.1 christos op.iplo_name[sizeof(op.iplo_name) - 1] = '\0';
512 1.1 christos
513 1.1 christos for (i = 0, l = backends; i < MAX_BACKENDS; i++, l++) {
514 1.1 christos if (op.iplo_type == (*l)->ipfl_type) {
515 1.1 christos err = (*(*l)->ipfl_table_del)(softc,
516 1.1 christos softl->ipf_back[i],
517 1.1 christos &op);
518 1.1 christos break;
519 1.1 christos }
520 1.1 christos }
521 1.1 christos
522 1.1 christos if (i == MAX_BACKENDS) {
523 1.1 christos IPFERROR(50030);
524 1.1 christos err = EINVAL;
525 1.1 christos }
526 1.1 christos return err;
527 1.1 christos }
528 1.1 christos
529 1.1 christos
530 1.1 christos /* ------------------------------------------------------------------------ */
531 1.1 christos /* Function: ipf_lookup_stats */
532 1.1 christos /* Returns: int - 0 = success, else error */
533 1.1 christos /* Parameters: softc(I) - pointer to soft context main structure */
534 1.1 christos /* data(I) - pointer to data from ioctl call */
535 1.1 christos /* */
536 1.1 christos /* Copy statistical information from inside the kernel back to user space. */
537 1.1 christos /* ------------------------------------------------------------------------ */
538 1.1 christos static int
539 1.2 christos ipf_lookup_stats(ipf_main_softc_t *softc, void *data)
540 1.1 christos {
541 1.1 christos ipf_lookup_softc_t *softl = softc->ipf_lookup_soft;
542 1.1 christos iplookupop_t op;
543 1.1 christos ipf_lookup_t **l;
544 1.1 christos int err;
545 1.1 christos int i;
546 1.1 christos
547 1.1 christos err = BCOPYIN(data, &op, sizeof(op));
548 1.1 christos if (err != 0) {
549 1.1 christos IPFERROR(50031);
550 1.1 christos return EFAULT;
551 1.1 christos }
552 1.1 christos
553 1.1 christos if ((op.iplo_unit < 0 || op.iplo_unit > IPL_LOGMAX) &&
554 1.1 christos (op.iplo_unit != IPLT_ALL)) {
555 1.1 christos IPFERROR(50032);
556 1.1 christos return EINVAL;
557 1.1 christos }
558 1.1 christos
559 1.1 christos for (i = 0, l = backends; i < MAX_BACKENDS; i++, l++) {
560 1.1 christos if (op.iplo_type == (*l)->ipfl_type) {
561 1.1 christos err = (*(*l)->ipfl_stats_get)(softc,
562 1.1 christos softl->ipf_back[i],
563 1.1 christos &op);
564 1.1 christos break;
565 1.1 christos }
566 1.1 christos }
567 1.1 christos
568 1.1 christos if (i == MAX_BACKENDS) {
569 1.1 christos IPFERROR(50033);
570 1.1 christos err = EINVAL;
571 1.1 christos }
572 1.1 christos
573 1.1 christos return err;
574 1.1 christos }
575 1.1 christos
576 1.1 christos
577 1.1 christos /* ------------------------------------------------------------------------ */
578 1.1 christos /* Function: ipf_lookup_flush */
579 1.1 christos /* Returns: int - 0 = success, else error */
580 1.1 christos /* Parameters: softc(I) - pointer to soft context main structure */
581 1.1 christos /* data(I) - pointer to data from ioctl call */
582 1.1 christos /* */
583 1.1 christos /* A flush is called when we want to flush all the nodes from a particular */
584 1.1 christos /* entry in the hash table/pool or want to remove all groups from those. */
585 1.1 christos /* ------------------------------------------------------------------------ */
586 1.1 christos static int
587 1.2 christos ipf_lookup_flush(ipf_main_softc_t *softc, void *data)
588 1.1 christos {
589 1.1 christos ipf_lookup_softc_t *softl = softc->ipf_lookup_soft;
590 1.1 christos int err, unit, num, type, i;
591 1.1 christos iplookupflush_t flush;
592 1.1 christos ipf_lookup_t **l;
593 1.1 christos
594 1.1 christos err = BCOPYIN(data, &flush, sizeof(flush));
595 1.1 christos if (err != 0) {
596 1.1 christos IPFERROR(50034);
597 1.1 christos return EFAULT;
598 1.1 christos }
599 1.1 christos
600 1.1 christos unit = flush.iplf_unit;
601 1.1 christos if ((unit < 0 || unit > IPL_LOGMAX) && (unit != IPLT_ALL)) {
602 1.1 christos IPFERROR(50035);
603 1.1 christos return EINVAL;
604 1.1 christos }
605 1.1 christos
606 1.1 christos flush.iplf_name[sizeof(flush.iplf_name) - 1] = '\0';
607 1.1 christos
608 1.1 christos type = flush.iplf_type;
609 1.1 christos IPFERROR(50036);
610 1.1 christos err = EINVAL;
611 1.1 christos num = 0;
612 1.1 christos
613 1.1 christos for (i = 0, l = backends; i < MAX_BACKENDS; i++, l++) {
614 1.1 christos if (type == (*l)->ipfl_type || type == IPLT_ALL) {
615 1.1 christos err = 0;
616 1.1 christos num += (*(*l)->ipfl_flush)(softc,
617 1.1 christos softl->ipf_back[i],
618 1.1 christos &flush);
619 1.1 christos }
620 1.1 christos }
621 1.1 christos
622 1.1 christos if (err == 0) {
623 1.1 christos flush.iplf_count = num;
624 1.1 christos err = BCOPYOUT(&flush, data, sizeof(flush));
625 1.1 christos if (err != 0) {
626 1.1 christos IPFERROR(50037);
627 1.1 christos err = EFAULT;
628 1.1 christos }
629 1.1 christos }
630 1.1 christos return err;
631 1.1 christos }
632 1.1 christos
633 1.1 christos
634 1.1 christos /* ------------------------------------------------------------------------ */
635 1.1 christos /* Function: ipf_lookup_delref */
636 1.1 christos /* Returns: void */
637 1.1 christos /* Parameters: softc(I) - pointer to soft context main structure */
638 1.1 christos /* type(I) - table type to operate on */
639 1.1 christos /* ptr(I) - pointer to object to remove reference for */
640 1.1 christos /* */
641 1.1 christos /* This function organises calling the correct deref function for a given */
642 1.1 christos /* type of object being passed into it. */
643 1.1 christos /* ------------------------------------------------------------------------ */
644 1.1 christos void
645 1.2 christos ipf_lookup_deref(ipf_main_softc_t *softc, int type, void *ptr)
646 1.1 christos {
647 1.1 christos ipf_lookup_softc_t *softl = softc->ipf_lookup_soft;
648 1.1 christos int i;
649 1.1 christos
650 1.1 christos if (ptr == NULL)
651 1.1 christos return;
652 1.1 christos
653 1.1 christos for (i = 0; i < MAX_BACKENDS; i++) {
654 1.1 christos if (type == backends[i]->ipfl_type) {
655 1.1 christos WRITE_ENTER(&softc->ipf_poolrw);
656 1.1 christos (*backends[i]->ipfl_table_deref)(softc,
657 1.1 christos softl->ipf_back[i],
658 1.1 christos ptr);
659 1.1 christos RWLOCK_EXIT(&softc->ipf_poolrw);
660 1.1 christos break;
661 1.1 christos }
662 1.1 christos }
663 1.1 christos }
664 1.1 christos
665 1.1 christos
666 1.1 christos /* ------------------------------------------------------------------------ */
667 1.1 christos /* Function: ipf_lookup_iterate */
668 1.1 christos /* Returns: int - 0 = success, else error */
669 1.1 christos /* Parameters: softc(I) - pointer to soft context main structure */
670 1.1 christos /* data(I) - pointer to data from ioctl call */
671 1.1 christos /* uid(I) - uid of caller */
672 1.1 christos /* ctx(I) - pointer to give the uid context */
673 1.1 christos /* */
674 1.1 christos /* Decodes ioctl request to step through either hash tables or pools. */
675 1.1 christos /* ------------------------------------------------------------------------ */
676 1.1 christos static int
677 1.2 christos ipf_lookup_iterate(ipf_main_softc_t *softc, void *data, int uid, void *ctx)
678 1.1 christos {
679 1.1 christos ipf_lookup_softc_t *softl = softc->ipf_lookup_soft;
680 1.1 christos ipflookupiter_t iter;
681 1.1 christos ipftoken_t *token;
682 1.1 christos int err, i;
683 1.1 christos SPL_INT(s);
684 1.1 christos
685 1.1 christos err = ipf_inobj(softc, data, NULL, &iter, IPFOBJ_LOOKUPITER);
686 1.1 christos if (err != 0)
687 1.1 christos return err;
688 1.1 christos
689 1.1 christos if (iter.ili_unit < IPL_LOGALL && iter.ili_unit > IPL_LOGMAX) {
690 1.1 christos IPFERROR(50038);
691 1.1 christos return EINVAL;
692 1.1 christos }
693 1.1 christos
694 1.1 christos if (iter.ili_ival != IPFGENITER_LOOKUP) {
695 1.1 christos IPFERROR(50039);
696 1.1 christos return EINVAL;
697 1.1 christos }
698 1.1 christos
699 1.1 christos SPL_SCHED(s);
700 1.1 christos token = ipf_token_find(softc, iter.ili_key, uid, ctx);
701 1.1 christos if (token == NULL) {
702 1.1 christos SPL_X(s);
703 1.1 christos IPFERROR(50040);
704 1.1 christos return ESRCH;
705 1.1 christos }
706 1.1 christos
707 1.1 christos for (i = 0; i < MAX_BACKENDS; i++) {
708 1.1 christos if (iter.ili_type == backends[i]->ipfl_type) {
709 1.1 christos err = (*backends[i]->ipfl_iter_next)(softc,
710 1.1 christos softl->ipf_back[i],
711 1.1 christos token, &iter);
712 1.1 christos break;
713 1.1 christos }
714 1.1 christos }
715 1.1 christos SPL_X(s);
716 1.1 christos
717 1.1 christos if (i == MAX_BACKENDS) {
718 1.1 christos IPFERROR(50041);
719 1.1 christos err = EINVAL;
720 1.1 christos }
721 1.1 christos
722 1.1 christos WRITE_ENTER(&softc->ipf_tokens);
723 1.1 christos ipf_token_deref(softc, token);
724 1.1 christos RWLOCK_EXIT(&softc->ipf_tokens);
725 1.1 christos
726 1.1 christos return err;
727 1.1 christos }
728 1.1 christos
729 1.1 christos
730 1.1 christos /* ------------------------------------------------------------------------ */
731 1.1 christos /* Function: ipf_lookup_iterderef */
732 1.1 christos /* Returns: void */
733 1.1 christos /* Parameters: softc(I) - pointer to soft context main structure */
734 1.1 christos /* type(I) - backend type to iterate through */
735 1.1 christos /* data(I) - pointer to data from ioctl call */
736 1.1 christos /* */
737 1.1 christos /* Decodes ioctl request to remove a particular hash table or pool and */
738 1.1 christos /* calls the relevant function to do the cleanup. */
739 1.1 christos /* Because each of the backend types has a different data structure, */
740 1.1 christos /* iteration is limited to one type at a time (i.e. it is not permitted to */
741 1.1 christos /* go on from pool types to hash types as part of the "get next".) */
742 1.1 christos /* ------------------------------------------------------------------------ */
743 1.1 christos void
744 1.2 christos ipf_lookup_iterderef(ipf_main_softc_t *softc, u_32_t type, void *data)
745 1.1 christos {
746 1.1 christos ipf_lookup_softc_t *softl = softc->ipf_lookup_soft;
747 1.1 christos struct iplookupiterkey *lkey;
748 1.1 christos iplookupiterkey_t key;
749 1.1 christos int i;
750 1.1 christos
751 1.1 christos key.ilik_key = type;
752 1.1 christos lkey = &key.ilik_unstr;
753 1.1 christos
754 1.1 christos if (lkey->ilik_ival != IPFGENITER_LOOKUP)
755 1.1 christos return;
756 1.1 christos
757 1.1 christos WRITE_ENTER(&softc->ipf_poolrw);
758 1.1 christos
759 1.1 christos for (i = 0; i < MAX_BACKENDS; i++) {
760 1.1 christos if (type == backends[i]->ipfl_type) {
761 1.1 christos (*backends[i]->ipfl_iter_deref)(softc,
762 1.1 christos softl->ipf_back[i],
763 1.1 christos lkey->ilik_otype,
764 1.1 christos lkey->ilik_unit,
765 1.1 christos data);
766 1.1 christos break;
767 1.1 christos }
768 1.1 christos }
769 1.1 christos RWLOCK_EXIT(&softc->ipf_poolrw);
770 1.1 christos }
771 1.1 christos
772 1.1 christos
773 1.1 christos /* ------------------------------------------------------------------------ */
774 1.1 christos /* Function: ipf_lookup_deltok */
775 1.1 christos /* Returns: int - 0 = success, else error */
776 1.1 christos /* Parameters: softc(I) - pointer to soft context main structure */
777 1.1 christos /* data(I) - pointer to data from ioctl call */
778 1.1 christos /* uid(I) - uid of caller */
779 1.1 christos /* ctx(I) - pointer to give the uid context */
780 1.1 christos /* */
781 1.1 christos /* Deletes the token identified by the combination of (type,uid,ctx) */
782 1.1 christos /* "key" is a combination of the table type, iterator type and the unit for */
783 1.1 christos /* which the token was being used. */
784 1.1 christos /* ------------------------------------------------------------------------ */
785 1.1 christos int
786 1.2 christos ipf_lookup_deltok(ipf_main_softc_t *softc, void *data, int uid, void *ctx)
787 1.1 christos {
788 1.1 christos int error, key;
789 1.1 christos SPL_INT(s);
790 1.1 christos
791 1.1 christos SPL_SCHED(s);
792 1.1 christos error = BCOPYIN(data, &key, sizeof(key));
793 1.1 christos if (error == 0)
794 1.1 christos error = ipf_token_del(softc, key, uid, ctx);
795 1.1 christos SPL_X(s);
796 1.1 christos return error;
797 1.1 christos }
798 1.1 christos
799 1.1 christos
800 1.1 christos /* ------------------------------------------------------------------------ */
801 1.1 christos /* Function: ipf_lookup_res_num */
802 1.1 christos /* Returns: void * - NULL = failure, else success. */
803 1.1 christos /* Parameters: softc(I) - pointer to soft context main structure */
804 1.1 christos /* unit(I) - device for which this is for */
805 1.1 christos /* type(I) - type of lookup these parameters are for. */
806 1.1 christos /* number(I) - table number to use when searching */
807 1.1 christos /* funcptr(IO) - pointer to pointer for storing IP address */
808 1.1 christos /* searching function. */
809 1.1 christos /* */
810 1.1 christos /* Search for the "table" number passed in amongst those configured for */
811 1.1 christos /* that particular type. If the type is recognised then the function to */
812 1.1 christos /* call to do the IP address search will be change, regardless of whether */
813 1.1 christos /* or not the "table" number exists. */
814 1.1 christos /* ------------------------------------------------------------------------ */
815 1.1 christos void *
816 1.2 christos ipf_lookup_res_num(ipf_main_softc_t *softc, int unit, u_int type, u_int number,
817 1.2 christos lookupfunc_t *funcptr)
818 1.1 christos {
819 1.1 christos char name[FR_GROUPLEN];
820 1.1 christos
821 1.1 christos #if defined(SNPRINTF) && defined(_KERNEL)
822 1.1 christos SNPRINTF(name, sizeof(name), "%u", number);
823 1.1 christos #else
824 1.1 christos (void) sprintf(name, "%u", number);
825 1.1 christos #endif
826 1.1 christos
827 1.1 christos return ipf_lookup_res_name(softc, unit, type, name, funcptr);
828 1.1 christos }
829 1.1 christos
830 1.1 christos
831 1.1 christos /* ------------------------------------------------------------------------ */
832 1.1 christos /* Function: ipf_lookup_res_name */
833 1.1 christos /* Returns: void * - NULL = failure, else success. */
834 1.1 christos /* Parameters: softc(I) - pointer to soft context main structure */
835 1.1 christos /* unit(I) - device for which this is for */
836 1.1 christos /* type(I) - type of lookup these parameters are for. */
837 1.1 christos /* name(I) - table name to use when searching */
838 1.1 christos /* funcptr(IO) - pointer to pointer for storing IP address */
839 1.1 christos /* searching function. */
840 1.1 christos /* */
841 1.1 christos /* Search for the "table" number passed in amongst those configured for */
842 1.1 christos /* that particular type. If the type is recognised then the function to */
843 1.1 christos /* call to do the IP address search will be change, regardless of whether */
844 1.1 christos /* or not the "table" number exists. */
845 1.1 christos /* ------------------------------------------------------------------------ */
846 1.1 christos void *
847 1.2 christos ipf_lookup_res_name(ipf_main_softc_t *softc, int unit, u_int type, char *name,
848 1.2 christos lookupfunc_t *funcptr)
849 1.1 christos {
850 1.1 christos ipf_lookup_softc_t *softl = softc->ipf_lookup_soft;
851 1.1 christos ipf_lookup_t **l;
852 1.1 christos void *ptr = NULL;
853 1.1 christos int i;
854 1.1 christos
855 1.1 christos READ_ENTER(&softc->ipf_poolrw);
856 1.1 christos
857 1.1 christos for (i = 0, l = backends; i < MAX_BACKENDS; i++, l++) {
858 1.1 christos if (type == (*l)->ipfl_type) {
859 1.1 christos ptr = (*(*l)->ipfl_select_add_ref)(softl->ipf_back[i],
860 1.1 christos unit, name);
861 1.1 christos if (ptr != NULL && funcptr != NULL) {
862 1.1 christos *funcptr = (*l)->ipfl_addr_find;
863 1.1 christos }
864 1.1 christos break;
865 1.1 christos }
866 1.1 christos }
867 1.1 christos
868 1.1 christos if (i == MAX_BACKENDS) {
869 1.1 christos ptr = NULL;
870 1.1 christos if (funcptr != NULL)
871 1.1 christos *funcptr = NULL;
872 1.1 christos }
873 1.1 christos
874 1.1 christos RWLOCK_EXIT(&softc->ipf_poolrw);
875 1.1 christos
876 1.1 christos return ptr;
877 1.1 christos }
878 1.1 christos
879 1.1 christos
880 1.1 christos /* ------------------------------------------------------------------------ */
881 1.1 christos /* Function: ipf_lookup_find_htable */
882 1.1 christos /* Returns: void * - NULL = failure, else success. */
883 1.1 christos /* Parameters: softc(I) - pointer to soft context main structure */
884 1.1 christos /* unit(I) - device for which this is for */
885 1.1 christos /* name(I) - table name to use when searching */
886 1.1 christos /* */
887 1.1 christos /* To support the group-map feature, where a hash table maps address */
888 1.1 christos /* networks to rule group numbers, we need to expose a function that uses */
889 1.1 christos /* only the hash table backend. */
890 1.1 christos /* ------------------------------------------------------------------------ */
891 1.1 christos void *
892 1.2 christos ipf_lookup_find_htable(ipf_main_softc_t *softc, int unit, char *name)
893 1.1 christos {
894 1.1 christos ipf_lookup_softc_t *softl = softc->ipf_lookup_soft;
895 1.1 christos ipf_lookup_t **l;
896 1.1 christos void *tab = NULL;
897 1.1 christos int i;
898 1.1 christos
899 1.1 christos READ_ENTER(&softc->ipf_poolrw);
900 1.1 christos
901 1.1 christos for (i = 0, l = backends; i < MAX_BACKENDS; i++, l++)
902 1.1 christos if (IPLT_HASH == (*l)->ipfl_type) {
903 1.1 christos tab = ipf_htable_find(softl->ipf_back[i], unit, name);
904 1.1 christos break;
905 1.1 christos }
906 1.1 christos
907 1.1 christos RWLOCK_EXIT(&softc->ipf_poolrw);
908 1.1 christos
909 1.1 christos return tab;
910 1.1 christos }
911 1.1 christos
912 1.1 christos
913 1.1 christos /* ------------------------------------------------------------------------ */
914 1.1 christos /* Function: ipf_lookup_sync */
915 1.1 christos /* Returns: void */
916 1.1 christos /* Parameters: softc(I) - pointer to soft context main structure */
917 1.1 christos /* */
918 1.1 christos /* This function is the interface that the machine dependent sync functions */
919 1.1 christos /* call when a network interface name change occurs. It then calls the sync */
920 1.1 christos /* functions of the lookup implementations - if they have one. */
921 1.1 christos /* ------------------------------------------------------------------------ */
922 1.1 christos /*ARGSUSED*/
923 1.1 christos void
924 1.2 christos ipf_lookup_sync(ipf_main_softc_t *softc, void *ifp)
925 1.1 christos {
926 1.1 christos ipf_lookup_softc_t *softl = softc->ipf_lookup_soft;
927 1.1 christos ipf_lookup_t **l;
928 1.1 christos int i;
929 1.1 christos
930 1.1 christos READ_ENTER(&softc->ipf_poolrw);
931 1.1 christos
932 1.1 christos for (i = 0, l = backends; i < MAX_BACKENDS; i++, l++)
933 1.1 christos if ((*l)->ipfl_sync != NULL)
934 1.1 christos (*(*l)->ipfl_sync)(softc, softl->ipf_back[i]);
935 1.1 christos
936 1.1 christos RWLOCK_EXIT(&softc->ipf_poolrw);
937 1.1 christos }
938 1.1 christos
939 1.1 christos
940 1.1 christos #ifndef _KERNEL
941 1.1 christos void
942 1.1 christos ipf_lookup_dump(softc, arg)
943 1.1 christos ipf_main_softc_t *softc;
944 1.1 christos void *arg;
945 1.1 christos {
946 1.1 christos ipf_lookup_softc_t *softl = softc->ipf_lookup_soft;
947 1.1 christos ipf_lookup_t **l;
948 1.1 christos int i;
949 1.1 christos
950 1.1 christos for (i = 0, l = backends; i < MAX_BACKENDS; i++, l++)
951 1.1 christos if (IPLT_POOL == (*l)->ipfl_type) {
952 1.1 christos ipf_pool_dump(softc, softl->ipf_back[i]);
953 1.1 christos break;
954 1.1 christos }
955 1.1 christos
956 1.1 christos for (i = 0, l = backends; i < MAX_BACKENDS; i++, l++)
957 1.1 christos if (IPLT_HASH == (*l)->ipfl_type) {
958 1.1 christos ipf_htable_dump(softc, softl->ipf_back[i]);
959 1.1 christos break;
960 1.1 christos }
961 1.1 christos }
962 1.1 christos #endif
963