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 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 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 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 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 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 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 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 ret = pfctl_table_add_addrs_h(pfh, tbl, addr, size, nadd, flags); 140 if (ret) { 141 errno = ret; 142 return (-1); 143 } 144 return (0); 145 } 146 147 int 148 pfr_del_addrs(struct pfr_table *tbl, struct pfr_addr *addr, int size, 149 int *ndel, int flags) 150 { 151 int ret; 152 153 ret = pfctl_table_del_addrs_h(pfh, tbl, addr, size, ndel, flags); 154 if (ret) { 155 errno = ret; 156 return (-1); 157 } 158 return (0); 159 } 160 161 int 162 pfr_set_addrs(struct pfr_table *tbl, struct pfr_addr *addr, int size, 163 int *nadd, int *ndel, int *nchange, int flags) 164 { 165 int ret; 166 167 ret = pfctl_table_set_addrs_h(pfh, tbl, addr, size, nadd, ndel, 168 nchange, flags); 169 if (ret) { 170 errno = ret; 171 return (-1); 172 } 173 return (0); 174 } 175 176 int 177 pfr_get_addrs(struct pfr_table *tbl, struct pfr_addr *addr, int *size, 178 int flags) 179 { 180 int ret; 181 182 ret = pfctl_table_get_addrs_h(pfh, tbl, addr, size, flags); 183 if (ret) { 184 errno = ret; 185 return (-1); 186 } 187 return (0); 188 } 189 190 int 191 pfr_get_astats(struct pfr_table *tbl, struct pfr_astats *addr, int *size, 192 int flags) 193 { 194 int ret; 195 196 ret = pfctl_get_astats(pfh, tbl, addr, size, flags); 197 if (ret) { 198 errno = ret; 199 return (-1); 200 } 201 return (0); 202 } 203 204 int 205 pfr_clr_astats(struct pfr_table *tbl, struct pfr_addr *addr, int size, 206 int *nzero, int flags) 207 { 208 int ret; 209 210 ret = pfctl_clr_astats(pfh, tbl, addr, size, nzero, flags); 211 if (ret != 0) 212 errno = ret; 213 214 return (ret); 215 } 216 217 int 218 pfr_tst_addrs(struct pfr_table *tbl, struct pfr_addr *addr, int size, 219 int *nmatch, int flags) 220 { 221 int ret; 222 223 if (tbl == NULL || size < 0 || (size && addr == NULL)) { 224 errno = EINVAL; 225 return (-1); 226 } 227 228 ret = pfctl_test_addrs(pfh, tbl, addr, size, nmatch, flags); 229 if (ret != 0) 230 errno = ret; 231 232 return (ret); 233 } 234 235 /* interface management code */ 236 237 int 238 pfi_get_ifaces(const char *filter, struct pfi_kif *buf, int *size) 239 { 240 struct pfioc_iface io; 241 242 if (size == NULL || *size < 0 || (*size && buf == NULL)) { 243 errno = EINVAL; 244 return (-1); 245 } 246 bzero(&io, sizeof io); 247 if (filter != NULL) 248 if (strlcpy(io.pfiio_name, filter, sizeof(io.pfiio_name)) >= 249 sizeof(io.pfiio_name)) { 250 errno = EINVAL; 251 return (-1); 252 } 253 io.pfiio_buffer = buf; 254 io.pfiio_esize = sizeof(*buf); 255 io.pfiio_size = *size; 256 if (ioctl(dev, DIOCIGETIFACES, &io)) 257 return (-1); 258 *size = io.pfiio_size; 259 return (0); 260 } 261 262 /* buffer management code */ 263 264 const size_t buf_esize[PFRB_MAX] = { 0, 265 sizeof(struct pfr_table), sizeof(struct pfr_tstats), 266 sizeof(struct pfr_addr), sizeof(struct pfr_astats), 267 sizeof(struct pfi_kif), sizeof(struct pfioc_trans_e) 268 }; 269 270 /* 271 * add one element to the buffer 272 */ 273 int 274 pfr_buf_add(struct pfr_buffer *b, const void *e) 275 { 276 size_t bs; 277 278 if (b == NULL || b->pfrb_type <= 0 || b->pfrb_type >= PFRB_MAX || 279 e == NULL) { 280 errno = EINVAL; 281 return (-1); 282 } 283 bs = buf_esize[b->pfrb_type]; 284 if (b->pfrb_size == b->pfrb_msize) 285 if (pfr_buf_grow(b, 0)) 286 return (-1); 287 memcpy(((caddr_t)b->pfrb_caddr) + bs * b->pfrb_size, e, bs); 288 b->pfrb_size++; 289 return (0); 290 } 291 292 /* 293 * return next element of the buffer (or first one if prev is NULL) 294 * see PFRB_FOREACH macro 295 */ 296 void * 297 pfr_buf_next(struct pfr_buffer *b, const void *prev) 298 { 299 size_t bs; 300 301 if (b == NULL || b->pfrb_type <= 0 || b->pfrb_type >= PFRB_MAX) 302 return (NULL); 303 if (b->pfrb_size == 0) 304 return (NULL); 305 if (prev == NULL) 306 return (b->pfrb_caddr); 307 bs = buf_esize[b->pfrb_type]; 308 if ((((caddr_t)prev)-((caddr_t)b->pfrb_caddr)) / bs >= b->pfrb_size-1) 309 return (NULL); 310 return (((caddr_t)prev) + bs); 311 } 312 313 /* 314 * minsize: 315 * 0: make the buffer somewhat bigger 316 * n: make room for "n" entries in the buffer 317 */ 318 int 319 pfr_buf_grow(struct pfr_buffer *b, int minsize) 320 { 321 caddr_t p; 322 size_t bs; 323 324 if (b == NULL || b->pfrb_type <= 0 || b->pfrb_type >= PFRB_MAX) { 325 errno = EINVAL; 326 return (-1); 327 } 328 if (minsize != 0 && minsize <= b->pfrb_msize) 329 return (0); 330 bs = buf_esize[b->pfrb_type]; 331 if (!b->pfrb_msize) { 332 if (minsize < 64) 333 minsize = 64; 334 } 335 if (minsize == 0) 336 minsize = b->pfrb_msize * 2; 337 p = reallocarray(b->pfrb_caddr, minsize, bs); 338 if (p == NULL) 339 return (-1); 340 bzero(p + b->pfrb_msize * bs, (minsize - b->pfrb_msize) * bs); 341 b->pfrb_caddr = p; 342 b->pfrb_msize = minsize; 343 return (0); 344 } 345 346 /* 347 * reset buffer and free memory. 348 */ 349 void 350 pfr_buf_clear(struct pfr_buffer *b) 351 { 352 if (b == NULL) 353 return; 354 free(b->pfrb_caddr); 355 b->pfrb_caddr = NULL; 356 b->pfrb_size = b->pfrb_msize = 0; 357 } 358 359 int 360 pfr_buf_load(struct pfr_buffer *b, char *file, int nonetwork, 361 int (*append_addr)(struct pfr_buffer *, char *, int, int), int opts) 362 { 363 FILE *fp; 364 char buf[BUF_SIZE]; 365 int rv; 366 367 if (file == NULL) 368 return (0); 369 if (!strcmp(file, "-")) 370 fp = stdin; 371 else { 372 fp = pfctl_fopen(file, "r"); 373 if (fp == NULL) 374 return (-1); 375 } 376 while ((rv = pfr_next_token(buf, fp)) == 1) 377 if (append_addr(b, buf, nonetwork, opts)) { 378 rv = -1; 379 break; 380 } 381 if (fp != stdin) 382 fclose(fp); 383 return (rv); 384 } 385 386 int 387 pfr_next_token(char buf[BUF_SIZE], FILE *fp) 388 { 389 static char next_ch = ' '; 390 int i = 0; 391 392 for (;;) { 393 /* skip spaces */ 394 while (isspace(next_ch) && !feof(fp)) 395 next_ch = fgetc(fp); 396 /* remove from '#' or ';' until end of line */ 397 if (next_ch == '#' || next_ch == ';') 398 while (!feof(fp)) { 399 next_ch = fgetc(fp); 400 if (next_ch == '\n') 401 break; 402 } 403 else 404 break; 405 } 406 if (feof(fp)) { 407 next_ch = ' '; 408 return (0); 409 } 410 do { 411 if (i < BUF_SIZE) 412 buf[i++] = next_ch; 413 next_ch = fgetc(fp); 414 } while (!feof(fp) && !isspace(next_ch)); 415 if (i >= BUF_SIZE) { 416 errno = EINVAL; 417 return (-1); 418 } 419 buf[i] = '\0'; 420 return (1); 421 } 422