2 /* $OpenBSD: pfctl_radix.c,v 1.27 2005/05/21 21:03:58 henning Exp $ */
5 * Copyright (c) 2002 Cedric Berger
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
12 * - Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * - Redistributions in binary form must reproduce the above
15 * copyright notice, this list of conditions and the following
16 * disclaimer in the documentation and/or other materials provided
17 * with the distribution.
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
22 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
23 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
24 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
25 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
26 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
27 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
29 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
34 #include <sys/types.h>
35 #include <sys/ioctl.h>
36 #include <sys/socket.h>
39 #include <net/pfvar.h>
55 static int pfr_next_token(char buf
[], FILE *);
59 pfr_clr_tables(struct pfr_table
*filter
, int *ndel
, int flags
)
61 struct pfioc_table io
;
63 bzero(&io
, sizeof io
);
64 io
.pfrio_flags
= flags
;
66 io
.pfrio_table
= *filter
;
67 if (ioctl(dev
, DIOCRCLRTABLES
, &io
))
70 *ndel
= io
.pfrio_ndel
;
75 pfr_add_tables(struct pfr_table
*tbl
, int size
, int *nadd
, int flags
)
77 struct pfioc_table io
;
79 if (size
< 0 || (size
&& tbl
== NULL
)) {
83 bzero(&io
, sizeof io
);
84 io
.pfrio_flags
= flags
;
85 io
.pfrio_buffer
= tbl
;
86 io
.pfrio_esize
= sizeof(*tbl
);
88 if (ioctl(dev
, DIOCRADDTABLES
, &io
))
91 *nadd
= io
.pfrio_nadd
;
96 pfr_del_tables(struct pfr_table
*tbl
, int size
, int *ndel
, int flags
)
98 struct pfioc_table io
;
100 if (size
< 0 || (size
&& tbl
== NULL
)) {
104 bzero(&io
, sizeof io
);
105 io
.pfrio_flags
= flags
;
106 io
.pfrio_buffer
= tbl
;
107 io
.pfrio_esize
= sizeof(*tbl
);
108 io
.pfrio_size
= size
;
109 if (ioctl(dev
, DIOCRDELTABLES
, &io
))
112 *ndel
= io
.pfrio_ndel
;
117 pfr_get_tables(struct pfr_table
*filter
, struct pfr_table
*tbl
, int *size
,
120 struct pfioc_table io
;
122 if (size
== NULL
|| *size
< 0 || (*size
&& tbl
== NULL
)) {
126 bzero(&io
, sizeof io
);
127 io
.pfrio_flags
= flags
;
129 io
.pfrio_table
= *filter
;
130 io
.pfrio_buffer
= tbl
;
131 io
.pfrio_esize
= sizeof(*tbl
);
132 io
.pfrio_size
= *size
;
133 if (ioctl(dev
, DIOCRGETTABLES
, &io
))
135 *size
= io
.pfrio_size
;
140 pfr_get_tstats(struct pfr_table
*filter
, struct pfr_tstats
*tbl
, int *size
,
143 struct pfioc_table io
;
145 if (size
== NULL
|| *size
< 0 || (*size
&& tbl
== NULL
)) {
149 bzero(&io
, sizeof io
);
150 io
.pfrio_flags
= flags
;
152 io
.pfrio_table
= *filter
;
153 io
.pfrio_buffer
= tbl
;
154 io
.pfrio_esize
= sizeof(*tbl
);
155 io
.pfrio_size
= *size
;
156 if (ioctl(dev
, DIOCRGETTSTATS
, &io
))
158 *size
= io
.pfrio_size
;
163 pfr_clr_addrs(struct pfr_table
*tbl
, int *ndel
, int flags
)
165 struct pfioc_table io
;
171 bzero(&io
, sizeof io
);
172 io
.pfrio_flags
= flags
;
173 io
.pfrio_table
= *tbl
;
174 if (ioctl(dev
, DIOCRCLRADDRS
, &io
))
177 *ndel
= io
.pfrio_ndel
;
182 pfr_add_addrs(struct pfr_table
*tbl
, struct pfr_addr
*addr
, int size
,
183 int *nadd
, int flags
)
185 struct pfioc_table io
;
187 if (tbl
== NULL
|| size
< 0 || (size
&& addr
== NULL
)) {
191 bzero(&io
, sizeof io
);
192 io
.pfrio_flags
= flags
;
193 io
.pfrio_table
= *tbl
;
194 io
.pfrio_buffer
= addr
;
195 io
.pfrio_esize
= sizeof(*addr
);
196 io
.pfrio_size
= size
;
197 if (ioctl(dev
, DIOCRADDADDRS
, &io
))
200 *nadd
= io
.pfrio_nadd
;
205 pfr_del_addrs(struct pfr_table
*tbl
, struct pfr_addr
*addr
, int size
,
206 int *ndel
, int flags
)
208 struct pfioc_table io
;
210 if (tbl
== NULL
|| size
< 0 || (size
&& addr
== NULL
)) {
214 bzero(&io
, sizeof io
);
215 io
.pfrio_flags
= flags
;
216 io
.pfrio_table
= *tbl
;
217 io
.pfrio_buffer
= addr
;
218 io
.pfrio_esize
= sizeof(*addr
);
219 io
.pfrio_size
= size
;
220 if (ioctl(dev
, DIOCRDELADDRS
, &io
))
223 *ndel
= io
.pfrio_ndel
;
228 pfr_set_addrs(struct pfr_table
*tbl
, struct pfr_addr
*addr
, int size
,
229 int *size2
, int *nadd
, int *ndel
, int *nchange
, int flags
)
231 struct pfioc_table io
;
233 if (tbl
== NULL
|| size
< 0 || (size
&& addr
== NULL
)) {
237 bzero(&io
, sizeof io
);
238 io
.pfrio_flags
= flags
;
239 io
.pfrio_table
= *tbl
;
240 io
.pfrio_buffer
= addr
;
241 io
.pfrio_esize
= sizeof(*addr
);
242 io
.pfrio_size
= size
;
243 io
.pfrio_size2
= (size2
!= NULL
) ? *size2
: 0;
244 if (ioctl(dev
, DIOCRSETADDRS
, &io
))
247 *nadd
= io
.pfrio_nadd
;
249 *ndel
= io
.pfrio_ndel
;
251 *nchange
= io
.pfrio_nchange
;
253 *size2
= io
.pfrio_size2
;
258 pfr_get_addrs(struct pfr_table
*tbl
, struct pfr_addr
*addr
, int *size
,
261 struct pfioc_table io
;
263 if (tbl
== NULL
|| size
== NULL
|| *size
< 0 ||
264 (*size
&& addr
== NULL
)) {
268 bzero(&io
, sizeof io
);
269 io
.pfrio_flags
= flags
;
270 io
.pfrio_table
= *tbl
;
271 io
.pfrio_buffer
= addr
;
272 io
.pfrio_esize
= sizeof(*addr
);
273 io
.pfrio_size
= *size
;
274 if (ioctl(dev
, DIOCRGETADDRS
, &io
))
276 *size
= io
.pfrio_size
;
281 pfr_get_astats(struct pfr_table
*tbl
, struct pfr_astats
*addr
, int *size
,
284 struct pfioc_table io
;
286 if (tbl
== NULL
|| size
== NULL
|| *size
< 0 ||
287 (*size
&& addr
== NULL
)) {
291 bzero(&io
, sizeof io
);
292 io
.pfrio_flags
= flags
;
293 io
.pfrio_table
= *tbl
;
294 io
.pfrio_buffer
= addr
;
295 io
.pfrio_esize
= sizeof(*addr
);
296 io
.pfrio_size
= *size
;
297 if (ioctl(dev
, DIOCRGETASTATS
, &io
))
299 *size
= io
.pfrio_size
;
304 pfr_clr_astats(struct pfr_table
*tbl
, struct pfr_addr
*addr
, int size
,
305 int *nzero
, int flags
)
307 struct pfioc_table io
;
309 if (tbl
== NULL
|| size
< 0 || (size
&& addr
== NULL
)) {
313 bzero(&io
, sizeof io
);
314 io
.pfrio_flags
= flags
;
315 io
.pfrio_table
= *tbl
;
316 io
.pfrio_buffer
= addr
;
317 io
.pfrio_esize
= sizeof(*addr
);
318 io
.pfrio_size
= size
;
319 if (ioctl(dev
, DIOCRCLRASTATS
, &io
))
322 *nzero
= io
.pfrio_nzero
;
327 pfr_clr_tstats(struct pfr_table
*tbl
, int size
, int *nzero
, int flags
)
329 struct pfioc_table io
;
331 if (size
< 0 || (size
&& !tbl
)) {
335 bzero(&io
, sizeof io
);
336 io
.pfrio_flags
= flags
;
337 io
.pfrio_buffer
= tbl
;
338 io
.pfrio_esize
= sizeof(*tbl
);
339 io
.pfrio_size
= size
;
340 if (ioctl(dev
, DIOCRCLRTSTATS
, &io
))
343 *nzero
= io
.pfrio_nzero
;
348 pfr_set_tflags(struct pfr_table
*tbl
, int size
, int setflag
, int clrflag
,
349 int *nchange
, int *ndel
, int flags
)
351 struct pfioc_table io
;
353 if (size
< 0 || (size
&& !tbl
)) {
357 bzero(&io
, sizeof io
);
358 io
.pfrio_flags
= flags
;
359 io
.pfrio_buffer
= tbl
;
360 io
.pfrio_esize
= sizeof(*tbl
);
361 io
.pfrio_size
= size
;
362 io
.pfrio_setflag
= setflag
;
363 io
.pfrio_clrflag
= clrflag
;
364 if (ioctl(dev
, DIOCRSETTFLAGS
, &io
))
367 *nchange
= io
.pfrio_nchange
;
369 *ndel
= io
.pfrio_ndel
;
374 pfr_tst_addrs(struct pfr_table
*tbl
, struct pfr_addr
*addr
, int size
,
375 int *nmatch
, int flags
)
377 struct pfioc_table io
;
379 if (tbl
== NULL
|| size
< 0 || (size
&& addr
== NULL
)) {
383 bzero(&io
, sizeof io
);
384 io
.pfrio_flags
= flags
;
385 io
.pfrio_table
= *tbl
;
386 io
.pfrio_buffer
= addr
;
387 io
.pfrio_esize
= sizeof(*addr
);
388 io
.pfrio_size
= size
;
389 if (ioctl(dev
, DIOCRTSTADDRS
, &io
))
392 *nmatch
= io
.pfrio_nmatch
;
397 pfr_ina_define(struct pfr_table
*tbl
, struct pfr_addr
*addr
, int size
,
398 int *nadd
, int *naddr
, int ticket
, int flags
)
400 struct pfioc_table io
;
402 if (tbl
== NULL
|| size
< 0 || (size
&& addr
== NULL
)) {
406 bzero(&io
, sizeof io
);
407 io
.pfrio_flags
= flags
;
408 io
.pfrio_table
= *tbl
;
409 io
.pfrio_buffer
= addr
;
410 io
.pfrio_esize
= sizeof(*addr
);
411 io
.pfrio_size
= size
;
412 io
.pfrio_ticket
= ticket
;
413 if (ioctl(dev
, DIOCRINADEFINE
, &io
))
416 *nadd
= io
.pfrio_nadd
;
418 *naddr
= io
.pfrio_naddr
;
422 /* interface management code */
425 pfi_get_ifaces(const char *filter
, struct pfi_kif
*buf
, int *size
)
427 struct pfioc_iface io
;
429 if (size
== NULL
|| *size
< 0 || (*size
&& buf
== NULL
)) {
433 bzero(&io
, sizeof io
);
435 if (strlcpy(io
.pfiio_name
, filter
, sizeof(io
.pfiio_name
)) >=
436 sizeof(io
.pfiio_name
)) {
440 io
.pfiio_buffer
= buf
;
441 io
.pfiio_esize
= sizeof(*buf
);
442 io
.pfiio_size
= *size
;
443 if (ioctl(dev
, DIOCIGETIFACES
, &io
))
445 *size
= io
.pfiio_size
;
449 /* buffer management code */
451 size_t buf_esize
[PFRB_MAX
] = { 0,
452 sizeof(struct pfr_table
), sizeof(struct pfr_tstats
),
453 sizeof(struct pfr_addr
), sizeof(struct pfr_astats
),
454 sizeof(struct pfi_kif
), sizeof(struct pfioc_trans_e
)
458 * add one element to the buffer
461 pfr_buf_add(struct pfr_buffer
*b
, const void *e
)
465 if (b
== NULL
|| b
->pfrb_type
<= 0 || b
->pfrb_type
>= PFRB_MAX
||
470 bs
= buf_esize
[b
->pfrb_type
];
471 if (b
->pfrb_size
== b
->pfrb_msize
)
472 if (pfr_buf_grow(b
, 0))
474 memcpy(((caddr_t
)b
->pfrb_caddr
) + bs
* b
->pfrb_size
, e
, bs
);
480 * return next element of the buffer (or first one if prev is NULL)
481 * see PFRB_FOREACH macro
484 pfr_buf_next(struct pfr_buffer
*b
, const void *prev
)
488 if (b
== NULL
|| b
->pfrb_type
<= 0 || b
->pfrb_type
>= PFRB_MAX
)
490 if (b
->pfrb_size
== 0)
493 return (b
->pfrb_caddr
);
494 bs
= buf_esize
[b
->pfrb_type
];
495 if ((((caddr_t
)prev
)-((caddr_t
)b
->pfrb_caddr
)) / bs
>= b
->pfrb_size
-1)
497 return (((caddr_t
)prev
) + bs
);
502 * 0: make the buffer somewhat bigger
503 * n: make room for "n" entries in the buffer
506 pfr_buf_grow(struct pfr_buffer
*b
, int minsize
)
511 if (b
== NULL
|| b
->pfrb_type
<= 0 || b
->pfrb_type
>= PFRB_MAX
) {
515 if (minsize
!= 0 && minsize
<= b
->pfrb_msize
)
517 bs
= buf_esize
[b
->pfrb_type
];
518 if (!b
->pfrb_msize
) {
521 b
->pfrb_caddr
= calloc(bs
, minsize
);
522 if (b
->pfrb_caddr
== NULL
)
524 b
->pfrb_msize
= minsize
;
527 minsize
= b
->pfrb_msize
* 2;
528 if (minsize
< 0 || minsize
>= SIZE_T_MAX
/ bs
) {
533 p
= realloc(b
->pfrb_caddr
, minsize
* bs
);
536 bzero(p
+ b
->pfrb_msize
* bs
, (minsize
- b
->pfrb_msize
) * bs
);
538 b
->pfrb_msize
= minsize
;
544 * reset buffer and free memory.
547 pfr_buf_clear(struct pfr_buffer
*b
)
551 if (b
->pfrb_caddr
!= NULL
)
553 b
->pfrb_caddr
= NULL
;
554 b
->pfrb_size
= b
->pfrb_msize
= 0;
558 pfr_buf_load(struct pfr_buffer
*b
, char *file
, int nonetwork
,
559 int (*append_addr
)(struct pfr_buffer
*, char *, int))
567 if (!strcmp(file
, "-"))
570 fp
= pfctl_fopen(file
, "r");
574 while ((rv
= pfr_next_token(buf
, fp
)) == 1)
575 if (append_addr(b
, buf
, nonetwork
)) {
585 pfr_next_token(char buf
[BUF_SIZE
], FILE *fp
)
587 static char next_ch
= ' ';
592 while (isspace((unsigned char)next_ch
) && !feof(fp
))
594 /* remove from '#' until end of line */
612 } while (!feof(fp
) && !isspace((unsigned char)next_ch
));
622 pfr_strerror(int errnum
)
626 return "Table does not exist";
628 return "Anchor or Ruleset does not exist";
630 return strerror(errnum
);