pfctl_radix.c revision 1.3 1 1.3 dsl /* $NetBSD: pfctl_radix.c,v 1.3 2004/10/29 19:46:27 dsl Exp $ */
2 1.1 itojun /* $OpenBSD: pfctl_radix.c,v 1.24 2004/02/10 18:29:30 henning Exp $ */
3 1.1 itojun
4 1.1 itojun /*
5 1.1 itojun * Copyright (c) 2002 Cedric Berger
6 1.1 itojun * All rights reserved.
7 1.1 itojun *
8 1.1 itojun * Redistribution and use in source and binary forms, with or without
9 1.1 itojun * modification, are permitted provided that the following conditions
10 1.1 itojun * are met:
11 1.1 itojun *
12 1.1 itojun * - Redistributions of source code must retain the above copyright
13 1.1 itojun * notice, this list of conditions and the following disclaimer.
14 1.1 itojun * - Redistributions in binary form must reproduce the above
15 1.1 itojun * copyright notice, this list of conditions and the following
16 1.1 itojun * disclaimer in the documentation and/or other materials provided
17 1.1 itojun * with the distribution.
18 1.1 itojun *
19 1.1 itojun * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 1.1 itojun * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 1.1 itojun * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
22 1.1 itojun * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
23 1.1 itojun * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
24 1.1 itojun * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
25 1.1 itojun * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
26 1.1 itojun * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
27 1.1 itojun * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 1.1 itojun * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
29 1.1 itojun * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 1.1 itojun * POSSIBILITY OF SUCH DAMAGE.
31 1.1 itojun *
32 1.1 itojun */
33 1.1 itojun
34 1.1 itojun #include <sys/types.h>
35 1.1 itojun #include <sys/ioctl.h>
36 1.1 itojun #include <sys/socket.h>
37 1.1 itojun
38 1.2 itojun #ifdef __NetBSD__
39 1.2 itojun #include <netinet/in.h>
40 1.2 itojun #endif
41 1.2 itojun
42 1.1 itojun #include <net/if.h>
43 1.1 itojun #include <net/pfvar.h>
44 1.1 itojun
45 1.1 itojun #include <errno.h>
46 1.1 itojun #include <string.h>
47 1.1 itojun #include <ctype.h>
48 1.1 itojun #include <stdio.h>
49 1.1 itojun #include <stdlib.h>
50 1.1 itojun #include <limits.h>
51 1.1 itojun #include <err.h>
52 1.1 itojun
53 1.1 itojun #include "pfctl.h"
54 1.1 itojun
55 1.1 itojun #define BUF_SIZE 256
56 1.1 itojun
57 1.1 itojun extern int dev;
58 1.1 itojun
59 1.1 itojun static int pfr_next_token(char buf[], FILE *);
60 1.1 itojun
61 1.1 itojun
62 1.1 itojun int
63 1.1 itojun pfr_clr_tables(struct pfr_table *filter, int *ndel, int flags)
64 1.1 itojun {
65 1.1 itojun struct pfioc_table io;
66 1.1 itojun
67 1.1 itojun bzero(&io, sizeof io);
68 1.1 itojun io.pfrio_flags = flags;
69 1.1 itojun if (filter != NULL)
70 1.1 itojun io.pfrio_table = *filter;
71 1.1 itojun if (ioctl(dev, DIOCRCLRTABLES, &io))
72 1.1 itojun return (-1);
73 1.1 itojun if (ndel != NULL)
74 1.1 itojun *ndel = io.pfrio_ndel;
75 1.1 itojun return (0);
76 1.1 itojun }
77 1.1 itojun
78 1.1 itojun int
79 1.1 itojun pfr_add_tables(struct pfr_table *tbl, int size, int *nadd, int flags)
80 1.1 itojun {
81 1.1 itojun struct pfioc_table io;
82 1.1 itojun
83 1.1 itojun if (size < 0 || (size && tbl == NULL)) {
84 1.1 itojun errno = EINVAL;
85 1.1 itojun return (-1);
86 1.1 itojun }
87 1.1 itojun bzero(&io, sizeof io);
88 1.1 itojun io.pfrio_flags = flags;
89 1.1 itojun io.pfrio_buffer = tbl;
90 1.1 itojun io.pfrio_esize = sizeof(*tbl);
91 1.1 itojun io.pfrio_size = size;
92 1.1 itojun if (ioctl(dev, DIOCRADDTABLES, &io))
93 1.1 itojun return (-1);
94 1.1 itojun if (nadd != NULL)
95 1.1 itojun *nadd = io.pfrio_nadd;
96 1.1 itojun return (0);
97 1.1 itojun }
98 1.1 itojun
99 1.1 itojun int
100 1.1 itojun pfr_del_tables(struct pfr_table *tbl, int size, int *ndel, int flags)
101 1.1 itojun {
102 1.1 itojun struct pfioc_table io;
103 1.1 itojun
104 1.1 itojun if (size < 0 || (size && tbl == NULL)) {
105 1.1 itojun errno = EINVAL;
106 1.1 itojun return (-1);
107 1.1 itojun }
108 1.1 itojun bzero(&io, sizeof io);
109 1.1 itojun io.pfrio_flags = flags;
110 1.1 itojun io.pfrio_buffer = tbl;
111 1.1 itojun io.pfrio_esize = sizeof(*tbl);
112 1.1 itojun io.pfrio_size = size;
113 1.1 itojun if (ioctl(dev, DIOCRDELTABLES, &io))
114 1.1 itojun return (-1);
115 1.1 itojun if (ndel != NULL)
116 1.1 itojun *ndel = io.pfrio_ndel;
117 1.1 itojun return (0);
118 1.1 itojun }
119 1.1 itojun
120 1.1 itojun int
121 1.1 itojun pfr_get_tables(struct pfr_table *filter, struct pfr_table *tbl, int *size,
122 1.1 itojun int flags)
123 1.1 itojun {
124 1.1 itojun struct pfioc_table io;
125 1.1 itojun
126 1.1 itojun if (size == NULL || *size < 0 || (*size && tbl == NULL)) {
127 1.1 itojun errno = EINVAL;
128 1.1 itojun return (-1);
129 1.1 itojun }
130 1.1 itojun bzero(&io, sizeof io);
131 1.1 itojun io.pfrio_flags = flags;
132 1.1 itojun if (filter != NULL)
133 1.1 itojun io.pfrio_table = *filter;
134 1.1 itojun io.pfrio_buffer = tbl;
135 1.1 itojun io.pfrio_esize = sizeof(*tbl);
136 1.1 itojun io.pfrio_size = *size;
137 1.1 itojun if (ioctl(dev, DIOCRGETTABLES, &io))
138 1.1 itojun return (-1);
139 1.1 itojun *size = io.pfrio_size;
140 1.1 itojun return (0);
141 1.1 itojun }
142 1.1 itojun
143 1.1 itojun int
144 1.1 itojun pfr_get_tstats(struct pfr_table *filter, struct pfr_tstats *tbl, int *size,
145 1.1 itojun int flags)
146 1.1 itojun {
147 1.1 itojun struct pfioc_table io;
148 1.1 itojun
149 1.1 itojun if (size == NULL || *size < 0 || (*size && tbl == NULL)) {
150 1.1 itojun errno = EINVAL;
151 1.1 itojun return (-1);
152 1.1 itojun }
153 1.1 itojun bzero(&io, sizeof io);
154 1.1 itojun io.pfrio_flags = flags;
155 1.1 itojun if (filter != NULL)
156 1.1 itojun io.pfrio_table = *filter;
157 1.1 itojun io.pfrio_buffer = tbl;
158 1.1 itojun io.pfrio_esize = sizeof(*tbl);
159 1.1 itojun io.pfrio_size = *size;
160 1.1 itojun if (ioctl(dev, DIOCRGETTSTATS, &io))
161 1.1 itojun return (-1);
162 1.1 itojun *size = io.pfrio_size;
163 1.1 itojun return (0);
164 1.1 itojun }
165 1.1 itojun
166 1.1 itojun int
167 1.1 itojun pfr_clr_addrs(struct pfr_table *tbl, int *ndel, int flags)
168 1.1 itojun {
169 1.1 itojun struct pfioc_table io;
170 1.1 itojun
171 1.1 itojun if (tbl == NULL) {
172 1.1 itojun errno = EINVAL;
173 1.1 itojun return (-1);
174 1.1 itojun }
175 1.1 itojun bzero(&io, sizeof io);
176 1.1 itojun io.pfrio_flags = flags;
177 1.1 itojun io.pfrio_table = *tbl;
178 1.1 itojun if (ioctl(dev, DIOCRCLRADDRS, &io))
179 1.1 itojun return (-1);
180 1.1 itojun if (ndel != NULL)
181 1.1 itojun *ndel = io.pfrio_ndel;
182 1.1 itojun return (0);
183 1.1 itojun }
184 1.1 itojun
185 1.1 itojun int
186 1.1 itojun pfr_add_addrs(struct pfr_table *tbl, struct pfr_addr *addr, int size,
187 1.1 itojun int *nadd, int flags)
188 1.1 itojun {
189 1.1 itojun struct pfioc_table io;
190 1.1 itojun
191 1.1 itojun if (tbl == NULL || size < 0 || (size && addr == NULL)) {
192 1.1 itojun errno = EINVAL;
193 1.1 itojun return (-1);
194 1.1 itojun }
195 1.1 itojun bzero(&io, sizeof io);
196 1.1 itojun io.pfrio_flags = flags;
197 1.1 itojun io.pfrio_table = *tbl;
198 1.1 itojun io.pfrio_buffer = addr;
199 1.1 itojun io.pfrio_esize = sizeof(*addr);
200 1.1 itojun io.pfrio_size = size;
201 1.1 itojun if (ioctl(dev, DIOCRADDADDRS, &io))
202 1.1 itojun return (-1);
203 1.1 itojun if (nadd != NULL)
204 1.1 itojun *nadd = io.pfrio_nadd;
205 1.1 itojun return (0);
206 1.1 itojun }
207 1.1 itojun
208 1.1 itojun int
209 1.1 itojun pfr_del_addrs(struct pfr_table *tbl, struct pfr_addr *addr, int size,
210 1.1 itojun int *ndel, int flags)
211 1.1 itojun {
212 1.1 itojun struct pfioc_table io;
213 1.1 itojun
214 1.1 itojun if (tbl == NULL || size < 0 || (size && addr == NULL)) {
215 1.1 itojun errno = EINVAL;
216 1.1 itojun return (-1);
217 1.1 itojun }
218 1.1 itojun bzero(&io, sizeof io);
219 1.1 itojun io.pfrio_flags = flags;
220 1.1 itojun io.pfrio_table = *tbl;
221 1.1 itojun io.pfrio_buffer = addr;
222 1.1 itojun io.pfrio_esize = sizeof(*addr);
223 1.1 itojun io.pfrio_size = size;
224 1.1 itojun if (ioctl(dev, DIOCRDELADDRS, &io))
225 1.1 itojun return (-1);
226 1.1 itojun if (ndel != NULL)
227 1.1 itojun *ndel = io.pfrio_ndel;
228 1.1 itojun return (0);
229 1.1 itojun }
230 1.1 itojun
231 1.1 itojun int
232 1.1 itojun pfr_set_addrs(struct pfr_table *tbl, struct pfr_addr *addr, int size,
233 1.1 itojun int *size2, int *nadd, int *ndel, int *nchange, int flags)
234 1.1 itojun {
235 1.1 itojun struct pfioc_table io;
236 1.1 itojun
237 1.1 itojun if (tbl == NULL || size < 0 || (size && addr == NULL)) {
238 1.1 itojun errno = EINVAL;
239 1.1 itojun return (-1);
240 1.1 itojun }
241 1.1 itojun bzero(&io, sizeof io);
242 1.1 itojun io.pfrio_flags = flags;
243 1.1 itojun io.pfrio_table = *tbl;
244 1.1 itojun io.pfrio_buffer = addr;
245 1.1 itojun io.pfrio_esize = sizeof(*addr);
246 1.1 itojun io.pfrio_size = size;
247 1.1 itojun io.pfrio_size2 = (size2 != NULL) ? *size2 : 0;
248 1.1 itojun if (ioctl(dev, DIOCRSETADDRS, &io))
249 1.1 itojun return (-1);
250 1.1 itojun if (nadd != NULL)
251 1.1 itojun *nadd = io.pfrio_nadd;
252 1.1 itojun if (ndel != NULL)
253 1.1 itojun *ndel = io.pfrio_ndel;
254 1.1 itojun if (nchange != NULL)
255 1.1 itojun *nchange = io.pfrio_nchange;
256 1.1 itojun if (size2 != NULL)
257 1.1 itojun *size2 = io.pfrio_size2;
258 1.1 itojun return (0);
259 1.1 itojun }
260 1.1 itojun
261 1.1 itojun int
262 1.1 itojun pfr_get_addrs(struct pfr_table *tbl, struct pfr_addr *addr, int *size,
263 1.1 itojun int flags)
264 1.1 itojun {
265 1.1 itojun struct pfioc_table io;
266 1.1 itojun
267 1.1 itojun if (tbl == NULL || size == NULL || *size < 0 ||
268 1.1 itojun (*size && addr == NULL)) {
269 1.1 itojun errno = EINVAL;
270 1.1 itojun return (-1);
271 1.1 itojun }
272 1.1 itojun bzero(&io, sizeof io);
273 1.1 itojun io.pfrio_flags = flags;
274 1.1 itojun io.pfrio_table = *tbl;
275 1.1 itojun io.pfrio_buffer = addr;
276 1.1 itojun io.pfrio_esize = sizeof(*addr);
277 1.1 itojun io.pfrio_size = *size;
278 1.1 itojun if (ioctl(dev, DIOCRGETADDRS, &io))
279 1.1 itojun return (-1);
280 1.1 itojun *size = io.pfrio_size;
281 1.1 itojun return (0);
282 1.1 itojun }
283 1.1 itojun
284 1.1 itojun int
285 1.1 itojun pfr_get_astats(struct pfr_table *tbl, struct pfr_astats *addr, int *size,
286 1.1 itojun int flags)
287 1.1 itojun {
288 1.1 itojun struct pfioc_table io;
289 1.1 itojun
290 1.1 itojun if (tbl == NULL || size == NULL || *size < 0 ||
291 1.1 itojun (*size && addr == NULL)) {
292 1.1 itojun errno = EINVAL;
293 1.1 itojun return (-1);
294 1.1 itojun }
295 1.1 itojun bzero(&io, sizeof io);
296 1.1 itojun io.pfrio_flags = flags;
297 1.1 itojun io.pfrio_table = *tbl;
298 1.1 itojun io.pfrio_buffer = addr;
299 1.1 itojun io.pfrio_esize = sizeof(*addr);
300 1.1 itojun io.pfrio_size = *size;
301 1.1 itojun if (ioctl(dev, DIOCRGETASTATS, &io))
302 1.1 itojun return (-1);
303 1.1 itojun *size = io.pfrio_size;
304 1.1 itojun return (0);
305 1.1 itojun }
306 1.1 itojun
307 1.1 itojun int
308 1.1 itojun pfr_clr_astats(struct pfr_table *tbl, struct pfr_addr *addr, int size,
309 1.1 itojun int *nzero, int flags)
310 1.1 itojun {
311 1.1 itojun struct pfioc_table io;
312 1.1 itojun
313 1.1 itojun if (tbl == NULL || size < 0 || (size && addr == NULL)) {
314 1.1 itojun errno = EINVAL;
315 1.1 itojun return (-1);
316 1.1 itojun }
317 1.1 itojun bzero(&io, sizeof io);
318 1.1 itojun io.pfrio_flags = flags;
319 1.1 itojun io.pfrio_table = *tbl;
320 1.1 itojun io.pfrio_buffer = addr;
321 1.1 itojun io.pfrio_esize = sizeof(*addr);
322 1.1 itojun io.pfrio_size = size;
323 1.1 itojun if (ioctl(dev, DIOCRCLRASTATS, &io))
324 1.1 itojun return (-1);
325 1.1 itojun if (nzero != NULL)
326 1.1 itojun *nzero = io.pfrio_nzero;
327 1.1 itojun return (0);
328 1.1 itojun }
329 1.1 itojun
330 1.1 itojun int
331 1.1 itojun pfr_clr_tstats(struct pfr_table *tbl, int size, int *nzero, int flags)
332 1.1 itojun {
333 1.1 itojun struct pfioc_table io;
334 1.1 itojun
335 1.1 itojun if (size < 0 || (size && !tbl)) {
336 1.1 itojun errno = EINVAL;
337 1.1 itojun return (-1);
338 1.1 itojun }
339 1.1 itojun bzero(&io, sizeof io);
340 1.1 itojun io.pfrio_flags = flags;
341 1.1 itojun io.pfrio_buffer = tbl;
342 1.1 itojun io.pfrio_esize = sizeof(*tbl);
343 1.1 itojun io.pfrio_size = size;
344 1.1 itojun if (ioctl(dev, DIOCRCLRTSTATS, &io))
345 1.1 itojun return (-1);
346 1.1 itojun if (nzero)
347 1.1 itojun *nzero = io.pfrio_nzero;
348 1.1 itojun return (0);
349 1.1 itojun }
350 1.1 itojun
351 1.1 itojun int
352 1.1 itojun pfr_set_tflags(struct pfr_table *tbl, int size, int setflag, int clrflag,
353 1.1 itojun int *nchange, int *ndel, int flags)
354 1.1 itojun {
355 1.1 itojun struct pfioc_table io;
356 1.1 itojun
357 1.1 itojun if (size < 0 || (size && !tbl)) {
358 1.1 itojun errno = EINVAL;
359 1.1 itojun return (-1);
360 1.1 itojun }
361 1.1 itojun bzero(&io, sizeof io);
362 1.1 itojun io.pfrio_flags = flags;
363 1.1 itojun io.pfrio_buffer = tbl;
364 1.1 itojun io.pfrio_esize = sizeof(*tbl);
365 1.1 itojun io.pfrio_size = size;
366 1.1 itojun io.pfrio_setflag = setflag;
367 1.1 itojun io.pfrio_clrflag = clrflag;
368 1.1 itojun if (ioctl(dev, DIOCRSETTFLAGS, &io))
369 1.1 itojun return (-1);
370 1.1 itojun if (nchange)
371 1.1 itojun *nchange = io.pfrio_nchange;
372 1.1 itojun if (ndel)
373 1.1 itojun *ndel = io.pfrio_ndel;
374 1.1 itojun return (0);
375 1.1 itojun }
376 1.1 itojun
377 1.1 itojun int
378 1.1 itojun pfr_tst_addrs(struct pfr_table *tbl, struct pfr_addr *addr, int size,
379 1.1 itojun int *nmatch, int flags)
380 1.1 itojun {
381 1.1 itojun struct pfioc_table io;
382 1.1 itojun
383 1.1 itojun if (tbl == NULL || size < 0 || (size && addr == NULL)) {
384 1.1 itojun errno = EINVAL;
385 1.1 itojun return (-1);
386 1.1 itojun }
387 1.1 itojun bzero(&io, sizeof io);
388 1.1 itojun io.pfrio_flags = flags;
389 1.1 itojun io.pfrio_table = *tbl;
390 1.1 itojun io.pfrio_buffer = addr;
391 1.1 itojun io.pfrio_esize = sizeof(*addr);
392 1.1 itojun io.pfrio_size = size;
393 1.1 itojun if (ioctl(dev, DIOCRTSTADDRS, &io))
394 1.1 itojun return (-1);
395 1.1 itojun if (nmatch)
396 1.1 itojun *nmatch = io.pfrio_nmatch;
397 1.1 itojun return (0);
398 1.1 itojun }
399 1.1 itojun
400 1.1 itojun int
401 1.1 itojun pfr_ina_begin(struct pfr_table *trs, int *ticket, int *ndel, int flags)
402 1.1 itojun {
403 1.1 itojun struct pfioc_table io;
404 1.1 itojun
405 1.1 itojun bzero(&io, sizeof io);
406 1.1 itojun if (trs != NULL)
407 1.1 itojun io.pfrio_table = *trs;
408 1.1 itojun io.pfrio_flags = flags;
409 1.1 itojun if (ioctl(dev, DIOCRINABEGIN, &io))
410 1.1 itojun return (-1);
411 1.1 itojun if (ndel != NULL)
412 1.1 itojun *ndel = io.pfrio_ndel;
413 1.1 itojun if (ticket != NULL)
414 1.1 itojun *ticket = io.pfrio_ticket;
415 1.1 itojun return (0);
416 1.1 itojun }
417 1.1 itojun
418 1.1 itojun int
419 1.1 itojun pfr_ina_commit(struct pfr_table *trs, int ticket, int *nadd, int *nchange,
420 1.1 itojun int flags)
421 1.1 itojun {
422 1.1 itojun struct pfioc_table io;
423 1.1 itojun
424 1.1 itojun bzero(&io, sizeof io);
425 1.1 itojun if (trs != NULL)
426 1.1 itojun io.pfrio_table = *trs;
427 1.1 itojun io.pfrio_flags = flags;
428 1.1 itojun io.pfrio_ticket = ticket;
429 1.1 itojun if (ioctl(dev, DIOCRINACOMMIT, &io))
430 1.1 itojun return (-1);
431 1.1 itojun if (nadd != NULL)
432 1.1 itojun *nadd = io.pfrio_nadd;
433 1.1 itojun if (nchange != NULL)
434 1.1 itojun *nchange = io.pfrio_nchange;
435 1.1 itojun return (0);
436 1.1 itojun }
437 1.1 itojun
438 1.1 itojun int
439 1.1 itojun pfr_ina_define(struct pfr_table *tbl, struct pfr_addr *addr, int size,
440 1.1 itojun int *nadd, int *naddr, int ticket, int flags)
441 1.1 itojun {
442 1.1 itojun struct pfioc_table io;
443 1.1 itojun
444 1.1 itojun if (tbl == NULL || size < 0 || (size && addr == NULL)) {
445 1.1 itojun errno = EINVAL;
446 1.1 itojun return (-1);
447 1.1 itojun }
448 1.1 itojun bzero(&io, sizeof io);
449 1.1 itojun io.pfrio_flags = flags;
450 1.1 itojun io.pfrio_table = *tbl;
451 1.1 itojun io.pfrio_buffer = addr;
452 1.1 itojun io.pfrio_esize = sizeof(*addr);
453 1.1 itojun io.pfrio_size = size;
454 1.1 itojun io.pfrio_ticket = ticket;
455 1.1 itojun if (ioctl(dev, DIOCRINADEFINE, &io))
456 1.1 itojun return (-1);
457 1.1 itojun if (nadd != NULL)
458 1.1 itojun *nadd = io.pfrio_nadd;
459 1.1 itojun if (naddr != NULL)
460 1.1 itojun *naddr = io.pfrio_naddr;
461 1.1 itojun return (0);
462 1.1 itojun }
463 1.1 itojun
464 1.1 itojun /* interface management code */
465 1.1 itojun
466 1.1 itojun int
467 1.1 itojun pfi_get_ifaces(const char *filter, struct pfi_if *buf, int *size, int flags)
468 1.1 itojun {
469 1.1 itojun struct pfioc_iface io;
470 1.1 itojun
471 1.1 itojun if (size == NULL || *size < 0 || (*size && buf == NULL)) {
472 1.1 itojun errno = EINVAL;
473 1.1 itojun return (-1);
474 1.1 itojun }
475 1.1 itojun bzero(&io, sizeof io);
476 1.1 itojun io.pfiio_flags = flags;
477 1.1 itojun if (filter != NULL)
478 1.1 itojun if (strlcpy(io.pfiio_name, filter, sizeof(io.pfiio_name)) >=
479 1.1 itojun sizeof(io.pfiio_name)) {
480 1.1 itojun errno = EINVAL;
481 1.1 itojun return (-1);
482 1.1 itojun }
483 1.1 itojun io.pfiio_buffer = buf;
484 1.1 itojun io.pfiio_esize = sizeof(*buf);
485 1.1 itojun io.pfiio_size = *size;
486 1.1 itojun if (ioctl(dev, DIOCIGETIFACES, &io))
487 1.1 itojun return (-1);
488 1.1 itojun *size = io.pfiio_size;
489 1.1 itojun return (0);
490 1.1 itojun }
491 1.1 itojun
492 1.1 itojun /* buffer management code */
493 1.1 itojun
494 1.1 itojun size_t buf_esize[PFRB_MAX] = { 0,
495 1.1 itojun sizeof(struct pfr_table), sizeof(struct pfr_tstats),
496 1.1 itojun sizeof(struct pfr_addr), sizeof(struct pfr_astats),
497 1.1 itojun sizeof(struct pfi_if), sizeof(struct pfioc_trans_e)
498 1.1 itojun };
499 1.1 itojun
500 1.1 itojun /*
501 1.1 itojun * add one element to the buffer
502 1.1 itojun */
503 1.1 itojun int
504 1.1 itojun pfr_buf_add(struct pfr_buffer *b, const void *e)
505 1.1 itojun {
506 1.1 itojun size_t bs;
507 1.1 itojun
508 1.1 itojun if (b == NULL || b->pfrb_type <= 0 || b->pfrb_type >= PFRB_MAX ||
509 1.1 itojun e == NULL) {
510 1.1 itojun errno = EINVAL;
511 1.1 itojun return (-1);
512 1.1 itojun }
513 1.1 itojun bs = buf_esize[b->pfrb_type];
514 1.1 itojun if (b->pfrb_size == b->pfrb_msize)
515 1.1 itojun if (pfr_buf_grow(b, 0))
516 1.1 itojun return (-1);
517 1.1 itojun memcpy(((caddr_t)b->pfrb_caddr) + bs * b->pfrb_size, e, bs);
518 1.1 itojun b->pfrb_size++;
519 1.1 itojun return (0);
520 1.1 itojun }
521 1.1 itojun
522 1.1 itojun /*
523 1.1 itojun * return next element of the buffer (or first one if prev is NULL)
524 1.1 itojun * see PFRB_FOREACH macro
525 1.1 itojun */
526 1.1 itojun void *
527 1.1 itojun pfr_buf_next(struct pfr_buffer *b, const void *prev)
528 1.1 itojun {
529 1.1 itojun size_t bs;
530 1.1 itojun
531 1.1 itojun if (b == NULL || b->pfrb_type <= 0 || b->pfrb_type >= PFRB_MAX)
532 1.1 itojun return (NULL);
533 1.1 itojun if (b->pfrb_size == 0)
534 1.1 itojun return (NULL);
535 1.1 itojun if (prev == NULL)
536 1.1 itojun return (b->pfrb_caddr);
537 1.1 itojun bs = buf_esize[b->pfrb_type];
538 1.1 itojun if ((((caddr_t)prev)-((caddr_t)b->pfrb_caddr)) / bs >= b->pfrb_size-1)
539 1.1 itojun return (NULL);
540 1.1 itojun return (((caddr_t)prev) + bs);
541 1.1 itojun }
542 1.1 itojun
543 1.1 itojun /*
544 1.1 itojun * minsize:
545 1.1 itojun * 0: make the buffer somewhat bigger
546 1.1 itojun * n: make room for "n" entries in the buffer
547 1.1 itojun */
548 1.1 itojun int
549 1.1 itojun pfr_buf_grow(struct pfr_buffer *b, int minsize)
550 1.1 itojun {
551 1.1 itojun caddr_t p;
552 1.1 itojun size_t bs;
553 1.1 itojun
554 1.1 itojun if (b == NULL || b->pfrb_type <= 0 || b->pfrb_type >= PFRB_MAX) {
555 1.1 itojun errno = EINVAL;
556 1.1 itojun return (-1);
557 1.1 itojun }
558 1.1 itojun if (minsize != 0 && minsize <= b->pfrb_msize)
559 1.1 itojun return (0);
560 1.1 itojun bs = buf_esize[b->pfrb_type];
561 1.1 itojun if (!b->pfrb_msize) {
562 1.1 itojun if (minsize < 64)
563 1.1 itojun minsize = 64;
564 1.1 itojun b->pfrb_caddr = calloc(bs, minsize);
565 1.1 itojun if (b->pfrb_caddr == NULL)
566 1.1 itojun return (-1);
567 1.1 itojun b->pfrb_msize = minsize;
568 1.1 itojun } else {
569 1.1 itojun if (minsize == 0)
570 1.1 itojun minsize = b->pfrb_msize * 2;
571 1.1 itojun if (minsize < 0 || minsize >= SIZE_T_MAX / bs) {
572 1.1 itojun /* msize overflow */
573 1.1 itojun errno = ENOMEM;
574 1.1 itojun return (-1);
575 1.1 itojun }
576 1.1 itojun p = realloc(b->pfrb_caddr, minsize * bs);
577 1.1 itojun if (p == NULL)
578 1.1 itojun return (-1);
579 1.1 itojun bzero(p + b->pfrb_msize * bs, (minsize - b->pfrb_msize) * bs);
580 1.1 itojun b->pfrb_caddr = p;
581 1.1 itojun b->pfrb_msize = minsize;
582 1.1 itojun }
583 1.1 itojun return (0);
584 1.1 itojun }
585 1.1 itojun
586 1.1 itojun /*
587 1.1 itojun * reset buffer and free memory.
588 1.1 itojun */
589 1.1 itojun void
590 1.1 itojun pfr_buf_clear(struct pfr_buffer *b)
591 1.1 itojun {
592 1.1 itojun if (b == NULL)
593 1.1 itojun return;
594 1.1 itojun if (b->pfrb_caddr != NULL)
595 1.1 itojun free(b->pfrb_caddr);
596 1.1 itojun b->pfrb_caddr = NULL;
597 1.1 itojun b->pfrb_size = b->pfrb_msize = 0;
598 1.1 itojun }
599 1.1 itojun
600 1.1 itojun int
601 1.1 itojun pfr_buf_load(struct pfr_buffer *b, char *file, int nonetwork,
602 1.1 itojun int (*append_addr)(struct pfr_buffer *, char *, int))
603 1.1 itojun {
604 1.1 itojun FILE *fp;
605 1.1 itojun char buf[BUF_SIZE];
606 1.1 itojun int rv;
607 1.1 itojun
608 1.1 itojun if (file == NULL)
609 1.1 itojun return (0);
610 1.1 itojun if (!strcmp(file, "-"))
611 1.1 itojun fp = stdin;
612 1.1 itojun else {
613 1.1 itojun fp = fopen(file, "r");
614 1.1 itojun if (fp == NULL)
615 1.1 itojun return (-1);
616 1.1 itojun }
617 1.1 itojun while ((rv = pfr_next_token(buf, fp)) == 1)
618 1.1 itojun if (append_addr(b, buf, nonetwork)) {
619 1.1 itojun rv = -1;
620 1.1 itojun break;
621 1.1 itojun }
622 1.1 itojun if (fp != stdin)
623 1.1 itojun fclose(fp);
624 1.1 itojun return (rv);
625 1.1 itojun }
626 1.1 itojun
627 1.1 itojun int
628 1.1 itojun pfr_next_token(char buf[BUF_SIZE], FILE *fp)
629 1.1 itojun {
630 1.1 itojun static char next_ch = ' ';
631 1.1 itojun int i = 0;
632 1.1 itojun
633 1.1 itojun for (;;) {
634 1.1 itojun /* skip spaces */
635 1.3 dsl while (isspace((unsigned char)next_ch) && !feof(fp))
636 1.1 itojun next_ch = fgetc(fp);
637 1.1 itojun /* remove from '#' until end of line */
638 1.1 itojun if (next_ch == '#')
639 1.1 itojun while (!feof(fp)) {
640 1.1 itojun next_ch = fgetc(fp);
641 1.1 itojun if (next_ch == '\n')
642 1.1 itojun break;
643 1.1 itojun }
644 1.1 itojun else
645 1.1 itojun break;
646 1.1 itojun }
647 1.1 itojun if (feof(fp)) {
648 1.1 itojun next_ch = ' ';
649 1.1 itojun return (0);
650 1.1 itojun }
651 1.1 itojun do {
652 1.1 itojun if (i < BUF_SIZE)
653 1.1 itojun buf[i++] = next_ch;
654 1.1 itojun next_ch = fgetc(fp);
655 1.3 dsl } while (!feof(fp) && !isspace((unsigned char)next_ch));
656 1.1 itojun if (i >= BUF_SIZE) {
657 1.1 itojun errno = EINVAL;
658 1.1 itojun return (-1);
659 1.1 itojun }
660 1.1 itojun buf[i] = '\0';
661 1.1 itojun return (1);
662 1.1 itojun }
663 1.1 itojun
664 1.1 itojun char *
665 1.1 itojun pfr_strerror(int errnum)
666 1.1 itojun {
667 1.1 itojun switch (errnum) {
668 1.1 itojun case ESRCH:
669 1.1 itojun return "Table does not exist";
670 1.1 itojun case ENOENT:
671 1.1 itojun return "Anchor or Ruleset does not exist";
672 1.1 itojun default:
673 1.1 itojun return strerror(errnum);
674 1.1 itojun }
675 1.1 itojun }
676