1 /* $OpenBSD: pfctl_radix.c,v 1.27 2005/05/21 21:03:58 henning Exp $ */
2
3 /*-
4 * SPDX-License-Identifier: BSD-2-Clause
5 *
6 * Copyright (c) 2002 Cedric Berger
7 * All rights reserved.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 *
13 * - Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * - Redistributions in binary form must reproduce the above
16 * copyright notice, this list of conditions and the following
17 * disclaimer in the documentation and/or other materials provided
18 * with the distribution.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
24 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
25 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
26 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
27 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
28 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
30 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31 * POSSIBILITY OF SUCH DAMAGE.
32 *
33 */
34
35 #include <sys/types.h>
36 #include <sys/ioctl.h>
37 #include <sys/socket.h>
38
39 #include <net/if.h>
40 #include <net/pfvar.h>
41
42 #include <errno.h>
43 #include <string.h>
44 #include <ctype.h>
45 #include <stdio.h>
46 #include <stdlib.h>
47 #include <limits.h>
48 #include <err.h>
49
50 #include "pfctl.h"
51 #include "pfctl_parser.h"
52
53 #define BUF_SIZE 256
54
55 extern int dev;
56
57 static int pfr_next_token(char buf[BUF_SIZE], FILE *);
58
59 struct pfr_ktablehead pfr_ktables = { 0 };
60 RB_GENERATE(pfr_ktablehead, pfr_ktable, pfrkt_tree, pfr_ktable_compare);
61
62 int
pfr_ktable_compare(struct pfr_ktable * p,struct pfr_ktable * q)63 pfr_ktable_compare(struct pfr_ktable *p, struct pfr_ktable *q)
64 {
65 int d;
66
67 if ((d = strncmp(p->pfrkt_name, q->pfrkt_name, PF_TABLE_NAME_SIZE)))
68 return (d);
69 return (strcmp(p->pfrkt_anchor, q->pfrkt_anchor));
70 }
71
72 static void
pfr_report_error(struct pfr_table * tbl,struct pfioc_table * io,const char * err)73 pfr_report_error(struct pfr_table *tbl, struct pfioc_table *io,
74 const char *err)
75 {
76 unsigned long maxcount;
77 size_t s;
78
79 s = sizeof(maxcount);
80 if (sysctlbyname("net.pf.request_maxcount", &maxcount, &s, NULL,
81 0) == -1)
82 return;
83
84 if (io->pfrio_size > maxcount || io->pfrio_size2 > maxcount)
85 fprintf(stderr, "cannot %s %s: too many elements.\n"
86 "Consider increasing net.pf.request_maxcount.",
87 err, tbl->pfrt_name);
88 }
89
90 int
pfr_add_table(struct pfr_table * tbl,int * nadd,int flags)91 pfr_add_table(struct pfr_table *tbl, int *nadd, int flags)
92 {
93 return (pfctl_add_table(pfh, tbl, nadd, flags));
94 }
95
96 int
pfr_del_table(struct pfr_table * tbl,int * ndel,int flags)97 pfr_del_table(struct pfr_table *tbl, int *ndel, int flags)
98 {
99 return (pfctl_del_table(pfh, tbl, ndel, flags));
100 }
101
102 int
pfr_get_tables(struct pfr_table * filter,struct pfr_table * tbl,int * size,int flags)103 pfr_get_tables(struct pfr_table *filter, struct pfr_table *tbl, int *size,
104 int flags)
105 {
106 struct pfioc_table io;
107
108 if (size == NULL || *size < 0 || (*size && tbl == NULL)) {
109 errno = EINVAL;
110 return (-1);
111 }
112 bzero(&io, sizeof io);
113 io.pfrio_flags = flags;
114 if (filter != NULL)
115 io.pfrio_table = *filter;
116 io.pfrio_buffer = tbl;
117 io.pfrio_esize = sizeof(*tbl);
118 io.pfrio_size = *size;
119 if (ioctl(dev, DIOCRGETTABLES, &io)) {
120 pfr_report_error(tbl, &io, "get table");
121 return (-1);
122 }
123 *size = io.pfrio_size;
124 return (0);
125 }
126
127 int
pfr_clr_addrs(struct pfr_table * tbl,int * ndel,int flags)128 pfr_clr_addrs(struct pfr_table *tbl, int *ndel, int flags)
129 {
130 return (pfctl_clear_addrs(pfh, tbl, ndel, flags));
131 }
132
133 int
pfr_add_addrs(struct pfr_table * tbl,struct pfr_addr * addr,int size,int * nadd,int flags)134 pfr_add_addrs(struct pfr_table *tbl, struct pfr_addr *addr, int size,
135 int *nadd, int flags)
136 {
137 int ret;
138
139 if (*nadd)
140 *nadd = 0;
141
142 ret = pfctl_table_add_addrs_h(pfh, tbl, addr, size, nadd, flags);
143 if (ret) {
144 errno = ret;
145 return (-1);
146 }
147 return (0);
148 }
149
150 int
pfr_del_addrs(struct pfr_table * tbl,struct pfr_addr * addr,int size,int * ndel,int flags)151 pfr_del_addrs(struct pfr_table *tbl, struct pfr_addr *addr, int size,
152 int *ndel, int flags)
153 {
154 int ret;
155
156 ret = pfctl_table_del_addrs_h(pfh, tbl, addr, size, ndel, flags);
157 if (ret) {
158 errno = ret;
159 return (-1);
160 }
161 return (0);
162 }
163
164 int
pfr_set_addrs(struct pfr_table * tbl,struct pfr_addr * addr,int size,int * nadd,int * ndel,int * nchange,int flags)165 pfr_set_addrs(struct pfr_table *tbl, struct pfr_addr *addr, int size,
166 int *nadd, int *ndel, int *nchange, int flags)
167 {
168 int ret;
169
170 ret = pfctl_table_set_addrs_h(pfh, tbl, addr, size, nadd, ndel,
171 nchange, flags);
172 if (ret) {
173 errno = ret;
174 return (-1);
175 }
176 return (0);
177 }
178
179 int
pfr_get_addrs(struct pfr_table * tbl,struct pfr_addr * addr,int * size,int flags)180 pfr_get_addrs(struct pfr_table *tbl, struct pfr_addr *addr, int *size,
181 int flags)
182 {
183 int ret;
184
185 ret = pfctl_table_get_addrs_h(pfh, tbl, addr, size, flags);
186 if (ret) {
187 errno = ret;
188 return (-1);
189 }
190 return (0);
191 }
192
193 int
pfr_get_astats(struct pfr_table * tbl,struct pfr_astats * addr,int * size,int flags)194 pfr_get_astats(struct pfr_table *tbl, struct pfr_astats *addr, int *size,
195 int flags)
196 {
197 return (pfctl_get_astats(pfh, tbl, addr, size, flags));
198 }
199
200 int
pfr_clr_astats(struct pfr_table * tbl,struct pfr_addr * addr,int size,int * nzero,int flags)201 pfr_clr_astats(struct pfr_table *tbl, struct pfr_addr *addr, int size,
202 int *nzero, int flags)
203 {
204 int ret;
205
206 ret = pfctl_clr_astats(pfh, tbl, addr, size, nzero, flags);
207 if (ret != 0)
208 errno = ret;
209
210 return (ret);
211 }
212
213 int
pfr_tst_addrs(struct pfr_table * tbl,struct pfr_addr * addr,int size,int * nmatch,int flags)214 pfr_tst_addrs(struct pfr_table *tbl, struct pfr_addr *addr, int size,
215 int *nmatch, int flags)
216 {
217 int ret;
218
219 if (tbl == NULL || size < 0 || (size && addr == NULL)) {
220 errno = EINVAL;
221 return (-1);
222 }
223
224 ret = pfctl_test_addrs(pfh, tbl, addr, size, nmatch, flags);
225 if (ret != 0)
226 errno = ret;
227
228 return (ret);
229 }
230
231 int
pfr_ina_define(struct pfr_table * tbl,struct pfr_addr * addr,int size,int * nadd,int * naddr,int ticket,int flags)232 pfr_ina_define(struct pfr_table *tbl, struct pfr_addr *addr, int size,
233 int *nadd, int *naddr, int ticket, int flags)
234 {
235 struct pfioc_table io;
236
237 if (tbl == NULL || size < 0 || (size && addr == NULL)) {
238 DBGPRINT("%s %p %d %p\n", __func__, tbl, size, addr);
239 errno = EINVAL;
240 return (-1);
241 }
242 bzero(&io, sizeof io);
243 io.pfrio_flags = flags;
244 io.pfrio_table = *tbl;
245 io.pfrio_buffer = addr;
246 io.pfrio_esize = sizeof(*addr);
247 io.pfrio_size = size;
248 io.pfrio_ticket = ticket;
249 if (ioctl(dev, DIOCRINADEFINE, &io)) {
250 pfr_report_error(tbl, &io, "define inactive set table");
251 return (-1);
252 }
253 if (nadd != NULL)
254 *nadd = io.pfrio_nadd;
255 if (naddr != NULL)
256 *naddr = io.pfrio_naddr;
257 return (0);
258 }
259
260 /* interface management code */
261
262 int
pfi_get_ifaces(const char * filter,struct pfi_kif * buf,int * size)263 pfi_get_ifaces(const char *filter, struct pfi_kif *buf, int *size)
264 {
265 struct pfioc_iface io;
266
267 if (size == NULL || *size < 0 || (*size && buf == NULL)) {
268 errno = EINVAL;
269 return (-1);
270 }
271 bzero(&io, sizeof io);
272 if (filter != NULL)
273 if (strlcpy(io.pfiio_name, filter, sizeof(io.pfiio_name)) >=
274 sizeof(io.pfiio_name)) {
275 errno = EINVAL;
276 return (-1);
277 }
278 io.pfiio_buffer = buf;
279 io.pfiio_esize = sizeof(*buf);
280 io.pfiio_size = *size;
281 if (ioctl(dev, DIOCIGETIFACES, &io))
282 return (-1);
283 *size = io.pfiio_size;
284 return (0);
285 }
286
287 /* buffer management code */
288
289 const size_t buf_esize[PFRB_MAX] = { 0,
290 sizeof(struct pfr_table), sizeof(struct pfr_tstats),
291 sizeof(struct pfr_addr), sizeof(struct pfr_astats),
292 sizeof(struct pfi_kif), sizeof(struct pfioc_trans_e)
293 };
294
295 /*
296 * add one element to the buffer
297 */
298 int
pfr_buf_add(struct pfr_buffer * b,const void * e)299 pfr_buf_add(struct pfr_buffer *b, const void *e)
300 {
301 size_t bs;
302
303 if (b == NULL || b->pfrb_type <= 0 || b->pfrb_type >= PFRB_MAX ||
304 e == NULL) {
305 errno = EINVAL;
306 return (-1);
307 }
308 bs = buf_esize[b->pfrb_type];
309 if (b->pfrb_size == b->pfrb_msize)
310 if (pfr_buf_grow(b, 0))
311 return (-1);
312 memcpy(((caddr_t)b->pfrb_caddr) + bs * b->pfrb_size, e, bs);
313 b->pfrb_size++;
314 return (0);
315 }
316
317 /*
318 * return next element of the buffer (or first one if prev is NULL)
319 * see PFRB_FOREACH macro
320 */
321 void *
pfr_buf_next(struct pfr_buffer * b,const void * prev)322 pfr_buf_next(struct pfr_buffer *b, const void *prev)
323 {
324 size_t bs;
325
326 if (b == NULL || b->pfrb_type <= 0 || b->pfrb_type >= PFRB_MAX)
327 return (NULL);
328 if (b->pfrb_size == 0)
329 return (NULL);
330 if (prev == NULL)
331 return (b->pfrb_caddr);
332 bs = buf_esize[b->pfrb_type];
333 if ((((caddr_t)prev)-((caddr_t)b->pfrb_caddr)) / bs >= b->pfrb_size-1)
334 return (NULL);
335 return (((caddr_t)prev) + bs);
336 }
337
338 /*
339 * minsize:
340 * 0: make the buffer somewhat bigger
341 * n: make room for "n" entries in the buffer
342 */
343 int
pfr_buf_grow(struct pfr_buffer * b,int minsize)344 pfr_buf_grow(struct pfr_buffer *b, int minsize)
345 {
346 caddr_t p;
347 size_t bs;
348
349 if (b == NULL || b->pfrb_type <= 0 || b->pfrb_type >= PFRB_MAX) {
350 errno = EINVAL;
351 return (-1);
352 }
353 if (minsize != 0 && minsize <= b->pfrb_msize)
354 return (0);
355 bs = buf_esize[b->pfrb_type];
356 if (!b->pfrb_msize) {
357 if (minsize < 64)
358 minsize = 64;
359 }
360 if (minsize == 0)
361 minsize = b->pfrb_msize * 2;
362 p = reallocarray(b->pfrb_caddr, minsize, bs);
363 if (p == NULL)
364 return (-1);
365 bzero(p + b->pfrb_msize * bs, (minsize - b->pfrb_msize) * bs);
366 b->pfrb_caddr = p;
367 b->pfrb_msize = minsize;
368 return (0);
369 }
370
371 /*
372 * reset buffer and free memory.
373 */
374 void
pfr_buf_clear(struct pfr_buffer * b)375 pfr_buf_clear(struct pfr_buffer *b)
376 {
377 if (b == NULL)
378 return;
379 free(b->pfrb_caddr);
380 b->pfrb_caddr = NULL;
381 b->pfrb_size = b->pfrb_msize = 0;
382 }
383
384 int
pfr_buf_load(struct pfr_buffer * b,char * file,int nonetwork,int (* append_addr)(struct pfr_buffer *,char *,int,int),int opts)385 pfr_buf_load(struct pfr_buffer *b, char *file, int nonetwork,
386 int (*append_addr)(struct pfr_buffer *, char *, int, int), int opts)
387 {
388 FILE *fp;
389 char buf[BUF_SIZE];
390 int rv;
391
392 if (file == NULL)
393 return (0);
394 if (!strcmp(file, "-"))
395 fp = stdin;
396 else {
397 fp = pfctl_fopen(file, "r");
398 if (fp == NULL)
399 return (-1);
400 }
401 while ((rv = pfr_next_token(buf, fp)) == 1)
402 if (append_addr(b, buf, nonetwork, opts)) {
403 rv = -1;
404 break;
405 }
406 if (fp != stdin)
407 fclose(fp);
408 return (rv);
409 }
410
411 int
pfr_next_token(char buf[BUF_SIZE],FILE * fp)412 pfr_next_token(char buf[BUF_SIZE], FILE *fp)
413 {
414 static char next_ch = ' ';
415 int i = 0;
416
417 for (;;) {
418 /* skip spaces */
419 while (isspace(next_ch) && !feof(fp))
420 next_ch = fgetc(fp);
421 /* remove from '#' or ';' until end of line */
422 if (next_ch == '#' || next_ch == ';')
423 while (!feof(fp)) {
424 next_ch = fgetc(fp);
425 if (next_ch == '\n')
426 break;
427 }
428 else
429 break;
430 }
431 if (feof(fp)) {
432 next_ch = ' ';
433 return (0);
434 }
435 do {
436 if (i < BUF_SIZE)
437 buf[i++] = next_ch;
438 next_ch = fgetc(fp);
439 } while (!feof(fp) && !isspace(next_ch));
440 if (i >= BUF_SIZE) {
441 errno = EINVAL;
442 return (-1);
443 }
444 buf[i] = '\0';
445 return (1);
446 }
447