xref: /freebsd/sbin/pfctl/pfctl_radix.c (revision 9e8d2962aad3af9bf1acd328a047f4745bb31086)
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 
52 #define BUF_SIZE 256
53 
54 extern int dev;
55 
56 static int	 pfr_next_token(char buf[BUF_SIZE], FILE *);
57 
58 static void
pfr_report_error(struct pfr_table * tbl,struct pfioc_table * io,const char * err)59 pfr_report_error(struct pfr_table *tbl, struct pfioc_table *io,
60     const char *err)
61 {
62 	unsigned long maxcount;
63 	size_t s;
64 
65 	s = sizeof(maxcount);
66 	if (sysctlbyname("net.pf.request_maxcount", &maxcount, &s, NULL,
67 	    0) == -1)
68 		return;
69 
70 	if (io->pfrio_size > maxcount || io->pfrio_size2 > maxcount)
71 		fprintf(stderr, "cannot %s %s: too many elements.\n"
72 		    "Consider increasing net.pf.request_maxcount.",
73 		    err, tbl->pfrt_name);
74 }
75 
76 int
pfr_add_table(struct pfr_table * tbl,int * nadd,int flags)77 pfr_add_table(struct pfr_table *tbl, int *nadd, int flags)
78 {
79 	return (pfctl_add_table(pfh, tbl, nadd, flags));
80 }
81 
82 int
pfr_del_table(struct pfr_table * tbl,int * ndel,int flags)83 pfr_del_table(struct pfr_table *tbl, int *ndel, int flags)
84 {
85 	return (pfctl_del_table(pfh, tbl, ndel, flags));
86 }
87 
88 int
pfr_get_tables(struct pfr_table * filter,struct pfr_table * tbl,int * size,int flags)89 pfr_get_tables(struct pfr_table *filter, struct pfr_table *tbl, int *size,
90 	int flags)
91 {
92 	struct pfioc_table io;
93 
94 	if (size == NULL || *size < 0 || (*size && tbl == NULL)) {
95 		errno = EINVAL;
96 		return (-1);
97 	}
98 	bzero(&io, sizeof io);
99 	io.pfrio_flags = flags;
100 	if (filter != NULL)
101 		io.pfrio_table = *filter;
102 	io.pfrio_buffer = tbl;
103 	io.pfrio_esize = sizeof(*tbl);
104 	io.pfrio_size = *size;
105 	if (ioctl(dev, DIOCRGETTABLES, &io)) {
106 		pfr_report_error(tbl, &io, "get table");
107 		return (-1);
108 	}
109 	*size = io.pfrio_size;
110 	return (0);
111 }
112 
113 int
pfr_clr_addrs(struct pfr_table * tbl,int * ndel,int flags)114 pfr_clr_addrs(struct pfr_table *tbl, int *ndel, int flags)
115 {
116 	struct pfioc_table io;
117 
118 	if (tbl == NULL) {
119 		errno = EINVAL;
120 		return (-1);
121 	}
122 	bzero(&io, sizeof io);
123 	io.pfrio_flags = flags;
124 	io.pfrio_table = *tbl;
125 	if (ioctl(dev, DIOCRCLRADDRS, &io))
126 		return (-1);
127 	if (ndel != NULL)
128 		*ndel = io.pfrio_ndel;
129 	return (0);
130 }
131 
132 int
pfr_add_addrs(struct pfr_table * tbl,struct pfr_addr * addr,int size,int * nadd,int flags)133 pfr_add_addrs(struct pfr_table *tbl, struct pfr_addr *addr, int size,
134     int *nadd, int flags)
135 {
136 	int ret;
137 
138 	ret = pfctl_table_add_addrs(dev, tbl, addr, size, nadd, flags);
139 	if (ret) {
140 		errno = ret;
141 		return (-1);
142 	}
143 	return (0);
144 }
145 
146 int
pfr_del_addrs(struct pfr_table * tbl,struct pfr_addr * addr,int size,int * ndel,int flags)147 pfr_del_addrs(struct pfr_table *tbl, struct pfr_addr *addr, int size,
148     int *ndel, int flags)
149 {
150 	int ret;
151 
152 	ret = pfctl_table_del_addrs(dev, tbl, addr, size, ndel, flags);
153 	if (ret) {
154 		errno = ret;
155 		return (-1);
156 	}
157 	return (0);
158 }
159 
160 int
pfr_set_addrs(struct pfr_table * tbl,struct pfr_addr * addr,int size,int * size2,int * nadd,int * ndel,int * nchange,int flags)161 pfr_set_addrs(struct pfr_table *tbl, struct pfr_addr *addr, int size,
162     int *size2, int *nadd, int *ndel, int *nchange, int flags)
163 {
164 	int ret;
165 
166 	ret = pfctl_table_set_addrs(dev, tbl, addr, size, size2, nadd, ndel,
167 	    nchange, flags);
168 	if (ret) {
169 		errno = ret;
170 		return (-1);
171 	}
172 	return (0);
173 }
174 
175 int
pfr_get_addrs(struct pfr_table * tbl,struct pfr_addr * addr,int * size,int flags)176 pfr_get_addrs(struct pfr_table *tbl, struct pfr_addr *addr, int *size,
177     int flags)
178 {
179 	int ret;
180 
181 	ret = pfctl_table_get_addrs(dev, tbl, addr, size, flags);
182 	if (ret) {
183 		errno = ret;
184 		return (-1);
185 	}
186 	return (0);
187 }
188 
189 int
pfr_get_astats(struct pfr_table * tbl,struct pfr_astats * addr,int * size,int flags)190 pfr_get_astats(struct pfr_table *tbl, struct pfr_astats *addr, int *size,
191     int flags)
192 {
193 	struct pfioc_table io;
194 
195 	if (tbl == NULL || size == NULL || *size < 0 ||
196 	    (*size && addr == NULL)) {
197 		errno = EINVAL;
198 		return (-1);
199 	}
200 	bzero(&io, sizeof io);
201 	io.pfrio_flags = flags;
202 	io.pfrio_table = *tbl;
203 	io.pfrio_buffer = addr;
204 	io.pfrio_esize = sizeof(*addr);
205 	io.pfrio_size = *size;
206 	if (ioctl(dev, DIOCRGETASTATS, &io)) {
207 		pfr_report_error(tbl, &io, "get astats from");
208 		return (-1);
209 	}
210 	*size = io.pfrio_size;
211 	return (0);
212 }
213 
214 int
pfr_clr_astats(struct pfr_table * tbl,struct pfr_addr * addr,int size,int * nzero,int flags)215 pfr_clr_astats(struct pfr_table *tbl, struct pfr_addr *addr, int size,
216     int *nzero, int flags)
217 {
218 	struct pfioc_table io;
219 
220 	if (size < 0 || !tbl || (size && !addr)) {
221 		errno = EINVAL;
222 		return (-1);
223 	}
224 	bzero(&io, sizeof io);
225 	io.pfrio_flags = flags;
226 	io.pfrio_table = *tbl;
227 	io.pfrio_buffer = addr;
228 	io.pfrio_esize = sizeof(*addr);
229 	io.pfrio_size = size;
230 	if (ioctl(dev, DIOCRCLRASTATS, &io) == -1)
231 		return (-1);
232 	if (nzero)
233 		*nzero = io.pfrio_nzero;
234 	return (0);
235 }
236 
237 int
pfr_clr_tstats(struct pfr_table * tbl,int size,int * nzero,int flags)238 pfr_clr_tstats(struct pfr_table *tbl, int size, int *nzero, int flags)
239 {
240 	struct pfioc_table io;
241 
242 	if (size < 0 || (size && !tbl)) {
243 		errno = EINVAL;
244 		return (-1);
245 	}
246 	bzero(&io, sizeof io);
247 	io.pfrio_flags = flags;
248 	io.pfrio_buffer = tbl;
249 	io.pfrio_esize = sizeof(*tbl);
250 	io.pfrio_size = size;
251 	if (ioctl(dev, DIOCRCLRTSTATS, &io)) {
252 		pfr_report_error(tbl, &io, "clear tstats from");
253 		return (-1);
254 	}
255 	if (nzero)
256 		*nzero = io.pfrio_nzero;
257 	return (0);
258 }
259 
260 int
pfr_tst_addrs(struct pfr_table * tbl,struct pfr_addr * addr,int size,int * nmatch,int flags)261 pfr_tst_addrs(struct pfr_table *tbl, struct pfr_addr *addr, int size,
262     int *nmatch, int flags)
263 {
264 	struct pfioc_table io;
265 
266 	if (tbl == NULL || size < 0 || (size && addr == NULL)) {
267 		errno = EINVAL;
268 		return (-1);
269 	}
270 	bzero(&io, sizeof io);
271 	io.pfrio_flags = flags;
272 	io.pfrio_table = *tbl;
273 	io.pfrio_buffer = addr;
274 	io.pfrio_esize = sizeof(*addr);
275 	io.pfrio_size = size;
276 	if (ioctl(dev, DIOCRTSTADDRS, &io)) {
277 		pfr_report_error(tbl, &io, "test addresses in");
278 		return (-1);
279 	}
280 	if (nmatch)
281 		*nmatch = io.pfrio_nmatch;
282 	return (0);
283 }
284 
285 int
pfr_ina_define(struct pfr_table * tbl,struct pfr_addr * addr,int size,int * nadd,int * naddr,int ticket,int flags)286 pfr_ina_define(struct pfr_table *tbl, struct pfr_addr *addr, int size,
287     int *nadd, int *naddr, int ticket, int flags)
288 {
289 	struct pfioc_table io;
290 
291 	if (tbl == NULL || size < 0 || (size && addr == NULL)) {
292 		errno = EINVAL;
293 		return (-1);
294 	}
295 	bzero(&io, sizeof io);
296 	io.pfrio_flags = flags;
297 	io.pfrio_table = *tbl;
298 	io.pfrio_buffer = addr;
299 	io.pfrio_esize = sizeof(*addr);
300 	io.pfrio_size = size;
301 	io.pfrio_ticket = ticket;
302 	if (ioctl(dev, DIOCRINADEFINE, &io)) {
303 		pfr_report_error(tbl, &io, "define inactive set table");
304 		return (-1);
305 	}
306 	if (nadd != NULL)
307 		*nadd = io.pfrio_nadd;
308 	if (naddr != NULL)
309 		*naddr = io.pfrio_naddr;
310 	return (0);
311 }
312 
313 /* interface management code */
314 
315 int
pfi_get_ifaces(const char * filter,struct pfi_kif * buf,int * size)316 pfi_get_ifaces(const char *filter, struct pfi_kif *buf, int *size)
317 {
318 	struct pfioc_iface io;
319 
320 	if (size == NULL || *size < 0 || (*size && buf == NULL)) {
321 		errno = EINVAL;
322 		return (-1);
323 	}
324 	bzero(&io, sizeof io);
325 	if (filter != NULL)
326 		if (strlcpy(io.pfiio_name, filter, sizeof(io.pfiio_name)) >=
327 		    sizeof(io.pfiio_name)) {
328 			errno = EINVAL;
329 			return (-1);
330 		}
331 	io.pfiio_buffer = buf;
332 	io.pfiio_esize = sizeof(*buf);
333 	io.pfiio_size = *size;
334 	if (ioctl(dev, DIOCIGETIFACES, &io))
335 		return (-1);
336 	*size = io.pfiio_size;
337 	return (0);
338 }
339 
340 /* buffer management code */
341 
342 const size_t buf_esize[PFRB_MAX] = { 0,
343 	sizeof(struct pfr_table), sizeof(struct pfr_tstats),
344 	sizeof(struct pfr_addr), sizeof(struct pfr_astats),
345 	sizeof(struct pfi_kif), sizeof(struct pfioc_trans_e)
346 };
347 
348 /*
349  * add one element to the buffer
350  */
351 int
pfr_buf_add(struct pfr_buffer * b,const void * e)352 pfr_buf_add(struct pfr_buffer *b, const void *e)
353 {
354 	size_t bs;
355 
356 	if (b == NULL || b->pfrb_type <= 0 || b->pfrb_type >= PFRB_MAX ||
357 	    e == NULL) {
358 		errno = EINVAL;
359 		return (-1);
360 	}
361 	bs = buf_esize[b->pfrb_type];
362 	if (b->pfrb_size == b->pfrb_msize)
363 		if (pfr_buf_grow(b, 0))
364 			return (-1);
365 	memcpy(((caddr_t)b->pfrb_caddr) + bs * b->pfrb_size, e, bs);
366 	b->pfrb_size++;
367 	return (0);
368 }
369 
370 /*
371  * return next element of the buffer (or first one if prev is NULL)
372  * see PFRB_FOREACH macro
373  */
374 void *
pfr_buf_next(struct pfr_buffer * b,const void * prev)375 pfr_buf_next(struct pfr_buffer *b, const void *prev)
376 {
377 	size_t bs;
378 
379 	if (b == NULL || b->pfrb_type <= 0 || b->pfrb_type >= PFRB_MAX)
380 		return (NULL);
381 	if (b->pfrb_size == 0)
382 		return (NULL);
383 	if (prev == NULL)
384 		return (b->pfrb_caddr);
385 	bs = buf_esize[b->pfrb_type];
386 	if ((((caddr_t)prev)-((caddr_t)b->pfrb_caddr)) / bs >= b->pfrb_size-1)
387 		return (NULL);
388 	return (((caddr_t)prev) + bs);
389 }
390 
391 /*
392  * minsize:
393  *    0: make the buffer somewhat bigger
394  *    n: make room for "n" entries in the buffer
395  */
396 int
pfr_buf_grow(struct pfr_buffer * b,int minsize)397 pfr_buf_grow(struct pfr_buffer *b, int minsize)
398 {
399 	caddr_t p;
400 	size_t bs;
401 
402 	if (b == NULL || b->pfrb_type <= 0 || b->pfrb_type >= PFRB_MAX) {
403 		errno = EINVAL;
404 		return (-1);
405 	}
406 	if (minsize != 0 && minsize <= b->pfrb_msize)
407 		return (0);
408 	bs = buf_esize[b->pfrb_type];
409 	if (!b->pfrb_msize) {
410 		if (minsize < 64)
411 			minsize = 64;
412 	}
413 	if (minsize == 0)
414 		minsize = b->pfrb_msize * 2;
415 	p = reallocarray(b->pfrb_caddr, minsize, bs);
416 	if (p == NULL)
417 		return (-1);
418 	bzero(p + b->pfrb_msize * bs, (minsize - b->pfrb_msize) * bs);
419 	b->pfrb_caddr = p;
420 	b->pfrb_msize = minsize;
421 	return (0);
422 }
423 
424 /*
425  * reset buffer and free memory.
426  */
427 void
pfr_buf_clear(struct pfr_buffer * b)428 pfr_buf_clear(struct pfr_buffer *b)
429 {
430 	if (b == NULL)
431 		return;
432 	if (b->pfrb_caddr != NULL)
433 		free(b->pfrb_caddr);
434 	b->pfrb_caddr = NULL;
435 	b->pfrb_size = b->pfrb_msize = 0;
436 }
437 
438 int
pfr_buf_load(struct pfr_buffer * b,char * file,int nonetwork,int (* append_addr)(struct pfr_buffer *,char *,int))439 pfr_buf_load(struct pfr_buffer *b, char *file, int nonetwork,
440     int (*append_addr)(struct pfr_buffer *, char *, int))
441 {
442 	FILE	*fp;
443 	char	 buf[BUF_SIZE];
444 	int	 rv;
445 
446 	if (file == NULL)
447 		return (0);
448 	if (!strcmp(file, "-"))
449 		fp = stdin;
450 	else {
451 		fp = pfctl_fopen(file, "r");
452 		if (fp == NULL)
453 			return (-1);
454 	}
455 	while ((rv = pfr_next_token(buf, fp)) == 1)
456 		if (append_addr(b, buf, nonetwork)) {
457 			rv = -1;
458 			break;
459 		}
460 	if (fp != stdin)
461 		fclose(fp);
462 	return (rv);
463 }
464 
465 int
pfr_next_token(char buf[BUF_SIZE],FILE * fp)466 pfr_next_token(char buf[BUF_SIZE], FILE *fp)
467 {
468 	static char	next_ch = ' ';
469 	int		i = 0;
470 
471 	for (;;) {
472 		/* skip spaces */
473 		while (isspace(next_ch) && !feof(fp))
474 			next_ch = fgetc(fp);
475 		/* remove from '#' or ';' until end of line */
476 		if (next_ch == '#' || next_ch == ';')
477 			while (!feof(fp)) {
478 				next_ch = fgetc(fp);
479 				if (next_ch == '\n')
480 					break;
481 			}
482 		else
483 			break;
484 	}
485 	if (feof(fp)) {
486 		next_ch = ' ';
487 		return (0);
488 	}
489 	do {
490 		if (i < BUF_SIZE)
491 			buf[i++] = next_ch;
492 		next_ch = fgetc(fp);
493 	} while (!feof(fp) && !isspace(next_ch));
494 	if (i >= BUF_SIZE) {
495 		errno = EINVAL;
496 		return (-1);
497 	}
498 	buf[i] = '\0';
499 	return (1);
500 }
501 
502 char *
pfr_strerror(int errnum)503 pfr_strerror(int errnum)
504 {
505 	switch (errnum) {
506 	case ESRCH:
507 		return "Table does not exist";
508 	case ENOENT:
509 		return "Anchor or Ruleset does not exist";
510 	default:
511 		return strerror(errnum);
512 	}
513 }
514