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