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