1 /* 2 * Copyright (c) 2002-2003 Luigi Rizzo 3 * Copyright (c) 1996 Alex Nash, Paul Traina, Poul-Henning Kamp 4 * Copyright (c) 1994 Ugen J.S.Antsilevich 5 * 6 * Idea and grammar partially left from: 7 * Copyright (c) 1993 Daniel Boulet 8 * 9 * Redistribution and use in source forms, with and without modification, 10 * are permitted provided that this entire comment appears intact. 11 * 12 * Redistribution in binary form may occur without any restrictions. 13 * Obviously, it would be nice if you gave credit where credit is due 14 * but requiring it would be too onerous. 15 * 16 * This software is provided ``AS IS'' without any warranties of any kind. 17 * 18 * NEW command line interface for IP firewall facility 19 * 20 * $FreeBSD$ 21 */ 22 23 #include <sys/types.h> 24 #include <sys/param.h> 25 #include <sys/socket.h> 26 #include <sys/sockio.h> 27 #include <sys/sysctl.h> 28 29 #include "ipfw2.h" 30 31 #include <ctype.h> 32 #include <err.h> 33 #include <errno.h> 34 #include <grp.h> 35 #include <netdb.h> 36 #include <pwd.h> 37 #include <stdio.h> 38 #include <stdarg.h> 39 #include <stdlib.h> 40 #include <string.h> 41 #include <sysexits.h> 42 #include <time.h> /* ctime */ 43 #include <timeconv.h> /* _long_to_time */ 44 #include <unistd.h> 45 #include <fcntl.h> 46 #include <stddef.h> /* offsetof */ 47 48 #include <net/ethernet.h> 49 #include <net/if.h> /* only IFNAMSIZ */ 50 #include <netinet/in.h> 51 #include <netinet/in_systm.h> /* only n_short, n_long */ 52 #include <netinet/ip.h> 53 #include <netinet/ip_icmp.h> 54 #include <netinet/ip_fw.h> 55 #include <netinet/tcp.h> 56 #include <arpa/inet.h> 57 58 struct cmdline_opts co; /* global options */ 59 60 struct format_opts { 61 int bcwidth; 62 int pcwidth; 63 int show_counters; 64 uint32_t set_mask; /* enabled sets mask */ 65 uint32_t flags; /* request flags */ 66 uint32_t first; /* first rule to request */ 67 uint32_t last; /* last rule to request */ 68 uint32_t dcnt; /* number of dynamic states */ 69 ipfw_obj_ctlv *tstate; /* table state data */ 70 }; 71 72 int resvd_set_number = RESVD_SET; 73 74 int ipfw_socket = -1; 75 76 #define CHECK_LENGTH(v, len) do { \ 77 if ((v) < (len)) \ 78 errx(EX_DATAERR, "Rule too long"); \ 79 } while (0) 80 /* 81 * Check if we have enough space in cmd buffer. Note that since 82 * first 8? u32 words are reserved by reserved header, full cmd 83 * buffer can't be used, so we need to protect from buffer overrun 84 * only. At the beginnig, cblen is less than actual buffer size by 85 * size of ipfw_insn_u32 instruction + 1 u32 work. This eliminates need 86 * for checking small instructions fitting in given range. 87 * We also (ab)use the fact that ipfw_insn is always the first field 88 * for any custom instruction. 89 */ 90 #define CHECK_CMDLEN CHECK_LENGTH(cblen, F_LEN((ipfw_insn *)cmd)) 91 92 #define GET_UINT_ARG(arg, min, max, tok, s_x) do { \ 93 if (!av[0]) \ 94 errx(EX_USAGE, "%s: missing argument", match_value(s_x, tok)); \ 95 if (_substrcmp(*av, "tablearg") == 0) { \ 96 arg = IP_FW_TARG; \ 97 break; \ 98 } \ 99 \ 100 { \ 101 long _xval; \ 102 char *end; \ 103 \ 104 _xval = strtol(*av, &end, 10); \ 105 \ 106 if (!isdigit(**av) || *end != '\0' || (_xval == 0 && errno == EINVAL)) \ 107 errx(EX_DATAERR, "%s: invalid argument: %s", \ 108 match_value(s_x, tok), *av); \ 109 \ 110 if (errno == ERANGE || _xval < min || _xval > max) \ 111 errx(EX_DATAERR, "%s: argument is out of range (%u..%u): %s", \ 112 match_value(s_x, tok), min, max, *av); \ 113 \ 114 if (_xval == IP_FW_TARG) \ 115 errx(EX_DATAERR, "%s: illegal argument value: %s", \ 116 match_value(s_x, tok), *av); \ 117 arg = _xval; \ 118 } \ 119 } while (0) 120 121 static struct _s_x f_tcpflags[] = { 122 { "syn", TH_SYN }, 123 { "fin", TH_FIN }, 124 { "ack", TH_ACK }, 125 { "psh", TH_PUSH }, 126 { "rst", TH_RST }, 127 { "urg", TH_URG }, 128 { "tcp flag", 0 }, 129 { NULL, 0 } 130 }; 131 132 static struct _s_x f_tcpopts[] = { 133 { "mss", IP_FW_TCPOPT_MSS }, 134 { "maxseg", IP_FW_TCPOPT_MSS }, 135 { "window", IP_FW_TCPOPT_WINDOW }, 136 { "sack", IP_FW_TCPOPT_SACK }, 137 { "ts", IP_FW_TCPOPT_TS }, 138 { "timestamp", IP_FW_TCPOPT_TS }, 139 { "cc", IP_FW_TCPOPT_CC }, 140 { "tcp option", 0 }, 141 { NULL, 0 } 142 }; 143 144 /* 145 * IP options span the range 0 to 255 so we need to remap them 146 * (though in fact only the low 5 bits are significant). 147 */ 148 static struct _s_x f_ipopts[] = { 149 { "ssrr", IP_FW_IPOPT_SSRR}, 150 { "lsrr", IP_FW_IPOPT_LSRR}, 151 { "rr", IP_FW_IPOPT_RR}, 152 { "ts", IP_FW_IPOPT_TS}, 153 { "ip option", 0 }, 154 { NULL, 0 } 155 }; 156 157 static struct _s_x f_iptos[] = { 158 { "lowdelay", IPTOS_LOWDELAY}, 159 { "throughput", IPTOS_THROUGHPUT}, 160 { "reliability", IPTOS_RELIABILITY}, 161 { "mincost", IPTOS_MINCOST}, 162 { "congestion", IPTOS_ECN_CE}, 163 { "ecntransport", IPTOS_ECN_ECT0}, 164 { "ip tos option", 0}, 165 { NULL, 0 } 166 }; 167 168 struct _s_x f_ipdscp[] = { 169 { "af11", IPTOS_DSCP_AF11 >> 2 }, /* 001010 */ 170 { "af12", IPTOS_DSCP_AF12 >> 2 }, /* 001100 */ 171 { "af13", IPTOS_DSCP_AF13 >> 2 }, /* 001110 */ 172 { "af21", IPTOS_DSCP_AF21 >> 2 }, /* 010010 */ 173 { "af22", IPTOS_DSCP_AF22 >> 2 }, /* 010100 */ 174 { "af23", IPTOS_DSCP_AF23 >> 2 }, /* 010110 */ 175 { "af31", IPTOS_DSCP_AF31 >> 2 }, /* 011010 */ 176 { "af32", IPTOS_DSCP_AF32 >> 2 }, /* 011100 */ 177 { "af33", IPTOS_DSCP_AF33 >> 2 }, /* 011110 */ 178 { "af41", IPTOS_DSCP_AF41 >> 2 }, /* 100010 */ 179 { "af42", IPTOS_DSCP_AF42 >> 2 }, /* 100100 */ 180 { "af43", IPTOS_DSCP_AF43 >> 2 }, /* 100110 */ 181 { "be", IPTOS_DSCP_CS0 >> 2 }, /* 000000 */ 182 { "ef", IPTOS_DSCP_EF >> 2 }, /* 101110 */ 183 { "cs0", IPTOS_DSCP_CS0 >> 2 }, /* 000000 */ 184 { "cs1", IPTOS_DSCP_CS1 >> 2 }, /* 001000 */ 185 { "cs2", IPTOS_DSCP_CS2 >> 2 }, /* 010000 */ 186 { "cs3", IPTOS_DSCP_CS3 >> 2 }, /* 011000 */ 187 { "cs4", IPTOS_DSCP_CS4 >> 2 }, /* 100000 */ 188 { "cs5", IPTOS_DSCP_CS5 >> 2 }, /* 101000 */ 189 { "cs6", IPTOS_DSCP_CS6 >> 2 }, /* 110000 */ 190 { "cs7", IPTOS_DSCP_CS7 >> 2 }, /* 100000 */ 191 { NULL, 0 } 192 }; 193 194 static struct _s_x limit_masks[] = { 195 {"all", DYN_SRC_ADDR|DYN_SRC_PORT|DYN_DST_ADDR|DYN_DST_PORT}, 196 {"src-addr", DYN_SRC_ADDR}, 197 {"src-port", DYN_SRC_PORT}, 198 {"dst-addr", DYN_DST_ADDR}, 199 {"dst-port", DYN_DST_PORT}, 200 {NULL, 0} 201 }; 202 203 /* 204 * we use IPPROTO_ETHERTYPE as a fake protocol id to call the print routines 205 * This is only used in this code. 206 */ 207 #define IPPROTO_ETHERTYPE 0x1000 208 static struct _s_x ether_types[] = { 209 /* 210 * Note, we cannot use "-:&/" in the names because they are field 211 * separators in the type specifications. Also, we use s = NULL as 212 * end-delimiter, because a type of 0 can be legal. 213 */ 214 { "ip", 0x0800 }, 215 { "ipv4", 0x0800 }, 216 { "ipv6", 0x86dd }, 217 { "arp", 0x0806 }, 218 { "rarp", 0x8035 }, 219 { "vlan", 0x8100 }, 220 { "loop", 0x9000 }, 221 { "trail", 0x1000 }, 222 { "at", 0x809b }, 223 { "atalk", 0x809b }, 224 { "aarp", 0x80f3 }, 225 { "pppoe_disc", 0x8863 }, 226 { "pppoe_sess", 0x8864 }, 227 { "ipx_8022", 0x00E0 }, 228 { "ipx_8023", 0x0000 }, 229 { "ipx_ii", 0x8137 }, 230 { "ipx_snap", 0x8137 }, 231 { "ipx", 0x8137 }, 232 { "ns", 0x0600 }, 233 { NULL, 0 } 234 }; 235 236 237 static struct _s_x rule_actions[] = { 238 { "accept", TOK_ACCEPT }, 239 { "pass", TOK_ACCEPT }, 240 { "allow", TOK_ACCEPT }, 241 { "permit", TOK_ACCEPT }, 242 { "count", TOK_COUNT }, 243 { "pipe", TOK_PIPE }, 244 { "queue", TOK_QUEUE }, 245 { "divert", TOK_DIVERT }, 246 { "tee", TOK_TEE }, 247 { "netgraph", TOK_NETGRAPH }, 248 { "ngtee", TOK_NGTEE }, 249 { "fwd", TOK_FORWARD }, 250 { "forward", TOK_FORWARD }, 251 { "skipto", TOK_SKIPTO }, 252 { "deny", TOK_DENY }, 253 { "drop", TOK_DENY }, 254 { "reject", TOK_REJECT }, 255 { "reset6", TOK_RESET6 }, 256 { "reset", TOK_RESET }, 257 { "unreach6", TOK_UNREACH6 }, 258 { "unreach", TOK_UNREACH }, 259 { "check-state", TOK_CHECKSTATE }, 260 { "//", TOK_COMMENT }, 261 { "nat", TOK_NAT }, 262 { "reass", TOK_REASS }, 263 { "setfib", TOK_SETFIB }, 264 { "setdscp", TOK_SETDSCP }, 265 { "call", TOK_CALL }, 266 { "return", TOK_RETURN }, 267 { NULL, 0 } /* terminator */ 268 }; 269 270 static struct _s_x rule_action_params[] = { 271 { "altq", TOK_ALTQ }, 272 { "log", TOK_LOG }, 273 { "tag", TOK_TAG }, 274 { "untag", TOK_UNTAG }, 275 { NULL, 0 } /* terminator */ 276 }; 277 278 /* 279 * The 'lookup' instruction accepts one of the following arguments. 280 * -1 is a terminator for the list. 281 * Arguments are passed as v[1] in O_DST_LOOKUP options. 282 */ 283 static int lookup_key[] = { 284 TOK_DSTIP, TOK_SRCIP, TOK_DSTPORT, TOK_SRCPORT, 285 TOK_UID, TOK_JAIL, TOK_DSCP, -1 }; 286 287 static struct _s_x rule_options[] = { 288 { "tagged", TOK_TAGGED }, 289 { "uid", TOK_UID }, 290 { "gid", TOK_GID }, 291 { "jail", TOK_JAIL }, 292 { "in", TOK_IN }, 293 { "limit", TOK_LIMIT }, 294 { "keep-state", TOK_KEEPSTATE }, 295 { "bridged", TOK_LAYER2 }, 296 { "layer2", TOK_LAYER2 }, 297 { "out", TOK_OUT }, 298 { "diverted", TOK_DIVERTED }, 299 { "diverted-loopback", TOK_DIVERTEDLOOPBACK }, 300 { "diverted-output", TOK_DIVERTEDOUTPUT }, 301 { "xmit", TOK_XMIT }, 302 { "recv", TOK_RECV }, 303 { "via", TOK_VIA }, 304 { "fragment", TOK_FRAG }, 305 { "frag", TOK_FRAG }, 306 { "fib", TOK_FIB }, 307 { "ipoptions", TOK_IPOPTS }, 308 { "ipopts", TOK_IPOPTS }, 309 { "iplen", TOK_IPLEN }, 310 { "ipid", TOK_IPID }, 311 { "ipprecedence", TOK_IPPRECEDENCE }, 312 { "dscp", TOK_DSCP }, 313 { "iptos", TOK_IPTOS }, 314 { "ipttl", TOK_IPTTL }, 315 { "ipversion", TOK_IPVER }, 316 { "ipver", TOK_IPVER }, 317 { "estab", TOK_ESTAB }, 318 { "established", TOK_ESTAB }, 319 { "setup", TOK_SETUP }, 320 { "sockarg", TOK_SOCKARG }, 321 { "tcpdatalen", TOK_TCPDATALEN }, 322 { "tcpflags", TOK_TCPFLAGS }, 323 { "tcpflgs", TOK_TCPFLAGS }, 324 { "tcpoptions", TOK_TCPOPTS }, 325 { "tcpopts", TOK_TCPOPTS }, 326 { "tcpseq", TOK_TCPSEQ }, 327 { "tcpack", TOK_TCPACK }, 328 { "tcpwin", TOK_TCPWIN }, 329 { "icmptype", TOK_ICMPTYPES }, 330 { "icmptypes", TOK_ICMPTYPES }, 331 { "dst-ip", TOK_DSTIP }, 332 { "src-ip", TOK_SRCIP }, 333 { "dst-port", TOK_DSTPORT }, 334 { "src-port", TOK_SRCPORT }, 335 { "proto", TOK_PROTO }, 336 { "MAC", TOK_MAC }, 337 { "mac", TOK_MAC }, 338 { "mac-type", TOK_MACTYPE }, 339 { "verrevpath", TOK_VERREVPATH }, 340 { "versrcreach", TOK_VERSRCREACH }, 341 { "antispoof", TOK_ANTISPOOF }, 342 { "ipsec", TOK_IPSEC }, 343 { "icmp6type", TOK_ICMP6TYPES }, 344 { "icmp6types", TOK_ICMP6TYPES }, 345 { "ext6hdr", TOK_EXT6HDR}, 346 { "flow-id", TOK_FLOWID}, 347 { "ipv6", TOK_IPV6}, 348 { "ip6", TOK_IPV6}, 349 { "ipv4", TOK_IPV4}, 350 { "ip4", TOK_IPV4}, 351 { "dst-ipv6", TOK_DSTIP6}, 352 { "dst-ip6", TOK_DSTIP6}, 353 { "src-ipv6", TOK_SRCIP6}, 354 { "src-ip6", TOK_SRCIP6}, 355 { "lookup", TOK_LOOKUP}, 356 { "flow", TOK_FLOW}, 357 { "//", TOK_COMMENT }, 358 359 { "not", TOK_NOT }, /* pseudo option */ 360 { "!", /* escape ? */ TOK_NOT }, /* pseudo option */ 361 { "or", TOK_OR }, /* pseudo option */ 362 { "|", /* escape */ TOK_OR }, /* pseudo option */ 363 { "{", TOK_STARTBRACE }, /* pseudo option */ 364 { "(", TOK_STARTBRACE }, /* pseudo option */ 365 { "}", TOK_ENDBRACE }, /* pseudo option */ 366 { ")", TOK_ENDBRACE }, /* pseudo option */ 367 { NULL, 0 } /* terminator */ 368 }; 369 370 void bprint_uint_arg(struct buf_pr *bp, const char *str, uint32_t arg); 371 static int ipfw_get_config(struct cmdline_opts *co, struct format_opts *fo, 372 ipfw_cfg_lheader **pcfg, size_t *psize); 373 static int ipfw_show_config(struct cmdline_opts *co, struct format_opts *fo, 374 ipfw_cfg_lheader *cfg, size_t sz, int ac, char **av); 375 static void ipfw_list_tifaces(void); 376 377 /* 378 * Simple string buffer API. 379 * Used to simplify buffer passing between function and for 380 * transparent overrun handling. 381 */ 382 383 /* 384 * Allocates new buffer of given size @sz. 385 * 386 * Returns 0 on success. 387 */ 388 int 389 bp_alloc(struct buf_pr *b, size_t size) 390 { 391 memset(b, 0, sizeof(struct buf_pr)); 392 393 if ((b->buf = calloc(1, size)) == NULL) 394 return (ENOMEM); 395 396 b->ptr = b->buf; 397 b->size = size; 398 b->avail = b->size; 399 400 return (0); 401 } 402 403 void 404 bp_free(struct buf_pr *b) 405 { 406 407 free(b->buf); 408 } 409 410 /* 411 * Flushes buffer so new writer start from beginning. 412 */ 413 void 414 bp_flush(struct buf_pr *b) 415 { 416 417 b->ptr = b->buf; 418 b->avail = b->size; 419 } 420 421 /* 422 * Print message specified by @format and args. 423 * Automatically manage buffer space and transparently handle 424 * buffer overruns. 425 * 426 * Returns number of bytes that should have been printed. 427 */ 428 int 429 bprintf(struct buf_pr *b, char *format, ...) 430 { 431 va_list args; 432 int i; 433 434 va_start(args, format); 435 436 i = vsnprintf(b->ptr, b->avail, format, args); 437 va_end(args); 438 439 if (i > b->avail || i < 0) { 440 /* Overflow or print error */ 441 b->avail = 0; 442 } else { 443 b->ptr += i; 444 b->avail -= i; 445 } 446 447 b->needed += i; 448 449 return (i); 450 } 451 452 /* 453 * Special values printer for tablearg-aware opcodes. 454 */ 455 void 456 bprint_uint_arg(struct buf_pr *bp, const char *str, uint32_t arg) 457 { 458 459 if (str != NULL) 460 bprintf(bp, "%s", str); 461 if (arg == IP_FW_TARG) 462 bprintf(bp, "tablearg"); 463 else 464 bprintf(bp, "%u", arg); 465 } 466 467 /* 468 * Helper routine to print a possibly unaligned uint64_t on 469 * various platform. If width > 0, print the value with 470 * the desired width, followed by a space; 471 * otherwise, return the required width. 472 */ 473 int 474 pr_u64(struct buf_pr *b, uint64_t *pd, int width) 475 { 476 #ifdef TCC 477 #define U64_FMT "I64" 478 #else 479 #define U64_FMT "llu" 480 #endif 481 uint64_t u; 482 unsigned long long d; 483 484 bcopy (pd, &u, sizeof(u)); 485 d = u; 486 return (width > 0) ? 487 bprintf(b, "%*" U64_FMT " ", width, d) : 488 snprintf(NULL, 0, "%" U64_FMT, d) ; 489 #undef U64_FMT 490 } 491 492 493 void * 494 safe_calloc(size_t number, size_t size) 495 { 496 void *ret = calloc(number, size); 497 498 if (ret == NULL) 499 err(EX_OSERR, "calloc"); 500 return ret; 501 } 502 503 void * 504 safe_realloc(void *ptr, size_t size) 505 { 506 void *ret = realloc(ptr, size); 507 508 if (ret == NULL) 509 err(EX_OSERR, "realloc"); 510 return ret; 511 } 512 513 /* 514 * Compare things like interface or table names. 515 */ 516 int 517 stringnum_cmp(const char *a, const char *b) 518 { 519 int la, lb; 520 521 la = strlen(a); 522 lb = strlen(b); 523 524 if (la > lb) 525 return (1); 526 else if (la < lb) 527 return (-01); 528 529 return (strcmp(a, b)); 530 } 531 532 533 /* 534 * conditionally runs the command. 535 * Selected options or negative -> getsockopt 536 */ 537 int 538 do_cmd(int optname, void *optval, uintptr_t optlen) 539 { 540 int i; 541 542 if (co.test_only) 543 return 0; 544 545 if (ipfw_socket == -1) 546 ipfw_socket = socket(AF_INET, SOCK_RAW, IPPROTO_RAW); 547 if (ipfw_socket < 0) 548 err(EX_UNAVAILABLE, "socket"); 549 550 if (optname == IP_FW_GET || optname == IP_DUMMYNET_GET || 551 optname == IP_FW_ADD || optname == IP_FW3 || 552 optname == IP_FW_NAT_GET_CONFIG || 553 optname < 0 || 554 optname == IP_FW_NAT_GET_LOG) { 555 if (optname < 0) 556 optname = -optname; 557 i = getsockopt(ipfw_socket, IPPROTO_IP, optname, optval, 558 (socklen_t *)optlen); 559 } else { 560 i = setsockopt(ipfw_socket, IPPROTO_IP, optname, optval, optlen); 561 } 562 return i; 563 } 564 565 /* 566 * do_set3 - pass ipfw control cmd to kernel 567 * @optname: option name 568 * @optval: pointer to option data 569 * @optlen: option length 570 * 571 * Assumes op3 header is already embedded. 572 * Calls setsockopt() with IP_FW3 as kernel-visible opcode. 573 * Returns 0 on success or errno otherwise. 574 */ 575 int 576 do_set3(int optname, ip_fw3_opheader *op3, uintptr_t optlen) 577 { 578 579 if (co.test_only) 580 return (0); 581 582 if (ipfw_socket == -1) 583 ipfw_socket = socket(AF_INET, SOCK_RAW, IPPROTO_RAW); 584 if (ipfw_socket < 0) 585 err(EX_UNAVAILABLE, "socket"); 586 587 op3->opcode = optname; 588 589 return (setsockopt(ipfw_socket, IPPROTO_IP, IP_FW3, op3, optlen)); 590 } 591 592 /* 593 * do_get3 - pass ipfw control cmd to kernel 594 * @optname: option name 595 * @optval: pointer to option data 596 * @optlen: pointer to option length 597 * 598 * Assumes op3 header is already embedded. 599 * Calls getsockopt() with IP_FW3 as kernel-visible opcode. 600 * Returns 0 on success or errno otherwise. 601 */ 602 int 603 do_get3(int optname, ip_fw3_opheader *op3, size_t *optlen) 604 { 605 int error; 606 607 if (co.test_only) 608 return (0); 609 610 if (ipfw_socket == -1) 611 ipfw_socket = socket(AF_INET, SOCK_RAW, IPPROTO_RAW); 612 if (ipfw_socket < 0) 613 err(EX_UNAVAILABLE, "socket"); 614 615 op3->opcode = optname; 616 617 error = getsockopt(ipfw_socket, IPPROTO_IP, IP_FW3, op3, 618 (socklen_t *)optlen); 619 620 return (error); 621 } 622 623 /** 624 * match_token takes a table and a string, returns the value associated 625 * with the string (-1 in case of failure). 626 */ 627 int 628 match_token(struct _s_x *table, char *string) 629 { 630 struct _s_x *pt; 631 uint i = strlen(string); 632 633 for (pt = table ; i && pt->s != NULL ; pt++) 634 if (strlen(pt->s) == i && !bcmp(string, pt->s, i)) 635 return pt->x; 636 return (-1); 637 } 638 639 /** 640 * match_token takes a table and a string, returns the value associated 641 * with the string for the best match. 642 * 643 * Returns: 644 * value from @table for matched records 645 * -1 for non-matched records 646 * -2 if more than one records match @string. 647 */ 648 int 649 match_token_relaxed(struct _s_x *table, char *string) 650 { 651 struct _s_x *pt, *m; 652 int i, c; 653 654 i = strlen(string); 655 c = 0; 656 657 for (pt = table ; i != 0 && pt->s != NULL ; pt++) { 658 if (strncmp(pt->s, string, i) != 0) 659 continue; 660 m = pt; 661 c++; 662 } 663 664 if (c == 1) 665 return (m->x); 666 667 return (c > 0 ? -2: -1); 668 } 669 670 /** 671 * match_value takes a table and a value, returns the string associated 672 * with the value (NULL in case of failure). 673 */ 674 char const * 675 match_value(struct _s_x *p, int value) 676 { 677 for (; p->s != NULL; p++) 678 if (p->x == value) 679 return p->s; 680 return NULL; 681 } 682 683 size_t 684 concat_tokens(char *buf, size_t bufsize, struct _s_x *table, char *delimiter) 685 { 686 struct _s_x *pt; 687 int l; 688 size_t sz; 689 690 for (sz = 0, pt = table ; pt->s != NULL; pt++) { 691 l = snprintf(buf + sz, bufsize - sz, "%s%s", 692 (sz == 0) ? "" : delimiter, pt->s); 693 sz += l; 694 bufsize += l; 695 if (sz > bufsize) 696 return (bufsize); 697 } 698 699 return (sz); 700 } 701 702 /* 703 * helper function to process a set of flags and set bits in the 704 * appropriate masks. 705 */ 706 int 707 fill_flags(struct _s_x *flags, char *p, char **e, uint32_t *set, 708 uint32_t *clear) 709 { 710 char *q; /* points to the separator */ 711 int val; 712 uint32_t *which; /* mask we are working on */ 713 714 while (p && *p) { 715 if (*p == '!') { 716 p++; 717 which = clear; 718 } else 719 which = set; 720 q = strchr(p, ','); 721 if (q) 722 *q++ = '\0'; 723 val = match_token(flags, p); 724 if (val <= 0) { 725 if (e != NULL) 726 *e = p; 727 return (-1); 728 } 729 *which |= (uint32_t)val; 730 p = q; 731 } 732 return (0); 733 } 734 735 void 736 print_flags_buffer(char *buf, size_t sz, struct _s_x *list, uint32_t set) 737 { 738 char const *comma = ""; 739 int i, l; 740 741 for (i = 0; list[i].x != 0; i++) { 742 if ((set & list[i].x) == 0) 743 continue; 744 745 set &= ~list[i].x; 746 l = snprintf(buf, sz, "%s%s", comma, list[i].s); 747 if (l >= sz) 748 return; 749 comma = ","; 750 buf += l; 751 sz -=l; 752 } 753 } 754 755 /* 756 * _substrcmp takes two strings and returns 1 if they do not match, 757 * and 0 if they match exactly or the first string is a sub-string 758 * of the second. A warning is printed to stderr in the case that the 759 * first string is a sub-string of the second. 760 * 761 * This function will be removed in the future through the usual 762 * deprecation process. 763 */ 764 int 765 _substrcmp(const char *str1, const char* str2) 766 { 767 768 if (strncmp(str1, str2, strlen(str1)) != 0) 769 return 1; 770 771 if (strlen(str1) != strlen(str2)) 772 warnx("DEPRECATED: '%s' matched '%s' as a sub-string", 773 str1, str2); 774 return 0; 775 } 776 777 /* 778 * _substrcmp2 takes three strings and returns 1 if the first two do not match, 779 * and 0 if they match exactly or the second string is a sub-string 780 * of the first. A warning is printed to stderr in the case that the 781 * first string does not match the third. 782 * 783 * This function exists to warn about the bizarre construction 784 * strncmp(str, "by", 2) which is used to allow people to use a shortcut 785 * for "bytes". The problem is that in addition to accepting "by", 786 * "byt", "byte", and "bytes", it also excepts "by_rabid_dogs" and any 787 * other string beginning with "by". 788 * 789 * This function will be removed in the future through the usual 790 * deprecation process. 791 */ 792 int 793 _substrcmp2(const char *str1, const char* str2, const char* str3) 794 { 795 796 if (strncmp(str1, str2, strlen(str2)) != 0) 797 return 1; 798 799 if (strcmp(str1, str3) != 0) 800 warnx("DEPRECATED: '%s' matched '%s'", 801 str1, str3); 802 return 0; 803 } 804 805 /* 806 * prints one port, symbolic or numeric 807 */ 808 static void 809 print_port(struct buf_pr *bp, int proto, uint16_t port) 810 { 811 812 if (proto == IPPROTO_ETHERTYPE) { 813 char const *s; 814 815 if (co.do_resolv && (s = match_value(ether_types, port)) ) 816 bprintf(bp, "%s", s); 817 else 818 bprintf(bp, "0x%04x", port); 819 } else { 820 struct servent *se = NULL; 821 if (co.do_resolv) { 822 struct protoent *pe = getprotobynumber(proto); 823 824 se = getservbyport(htons(port), pe ? pe->p_name : NULL); 825 } 826 if (se) 827 bprintf(bp, "%s", se->s_name); 828 else 829 bprintf(bp, "%d", port); 830 } 831 } 832 833 static struct _s_x _port_name[] = { 834 {"dst-port", O_IP_DSTPORT}, 835 {"src-port", O_IP_SRCPORT}, 836 {"ipid", O_IPID}, 837 {"iplen", O_IPLEN}, 838 {"ipttl", O_IPTTL}, 839 {"mac-type", O_MAC_TYPE}, 840 {"tcpdatalen", O_TCPDATALEN}, 841 {"tcpwin", O_TCPWIN}, 842 {"tagged", O_TAGGED}, 843 {NULL, 0} 844 }; 845 846 /* 847 * Print the values in a list 16-bit items of the types above. 848 * XXX todo: add support for mask. 849 */ 850 static void 851 print_newports(struct buf_pr *bp, ipfw_insn_u16 *cmd, int proto, int opcode) 852 { 853 uint16_t *p = cmd->ports; 854 int i; 855 char const *sep; 856 857 if (opcode != 0) { 858 sep = match_value(_port_name, opcode); 859 if (sep == NULL) 860 sep = "???"; 861 bprintf(bp, " %s", sep); 862 } 863 sep = " "; 864 for (i = F_LEN((ipfw_insn *)cmd) - 1; i > 0; i--, p += 2) { 865 bprintf(bp, "%s", sep); 866 print_port(bp, proto, p[0]); 867 if (p[0] != p[1]) { 868 bprintf(bp, "-"); 869 print_port(bp, proto, p[1]); 870 } 871 sep = ","; 872 } 873 } 874 875 /* 876 * Like strtol, but also translates service names into port numbers 877 * for some protocols. 878 * In particular: 879 * proto == -1 disables the protocol check; 880 * proto == IPPROTO_ETHERTYPE looks up an internal table 881 * proto == <some value in /etc/protocols> matches the values there. 882 * Returns *end == s in case the parameter is not found. 883 */ 884 static int 885 strtoport(char *s, char **end, int base, int proto) 886 { 887 char *p, *buf; 888 char *s1; 889 int i; 890 891 *end = s; /* default - not found */ 892 if (*s == '\0') 893 return 0; /* not found */ 894 895 if (isdigit(*s)) 896 return strtol(s, end, base); 897 898 /* 899 * find separator. '\\' escapes the next char. 900 */ 901 for (s1 = s; *s1 && (isalnum(*s1) || *s1 == '\\') ; s1++) 902 if (*s1 == '\\' && s1[1] != '\0') 903 s1++; 904 905 buf = safe_calloc(s1 - s + 1, 1); 906 907 /* 908 * copy into a buffer skipping backslashes 909 */ 910 for (p = s, i = 0; p != s1 ; p++) 911 if (*p != '\\') 912 buf[i++] = *p; 913 buf[i++] = '\0'; 914 915 if (proto == IPPROTO_ETHERTYPE) { 916 i = match_token(ether_types, buf); 917 free(buf); 918 if (i != -1) { /* found */ 919 *end = s1; 920 return i; 921 } 922 } else { 923 struct protoent *pe = NULL; 924 struct servent *se; 925 926 if (proto != 0) 927 pe = getprotobynumber(proto); 928 setservent(1); 929 se = getservbyname(buf, pe ? pe->p_name : NULL); 930 free(buf); 931 if (se != NULL) { 932 *end = s1; 933 return ntohs(se->s_port); 934 } 935 } 936 return 0; /* not found */ 937 } 938 939 /* 940 * Fill the body of the command with the list of port ranges. 941 */ 942 static int 943 fill_newports(ipfw_insn_u16 *cmd, char *av, int proto, int cblen) 944 { 945 uint16_t a, b, *p = cmd->ports; 946 int i = 0; 947 char *s = av; 948 949 while (*s) { 950 a = strtoport(av, &s, 0, proto); 951 if (s == av) /* empty or invalid argument */ 952 return (0); 953 954 CHECK_LENGTH(cblen, i + 2); 955 956 switch (*s) { 957 case '-': /* a range */ 958 av = s + 1; 959 b = strtoport(av, &s, 0, proto); 960 /* Reject expressions like '1-abc' or '1-2-3'. */ 961 if (s == av || (*s != ',' && *s != '\0')) 962 return (0); 963 p[0] = a; 964 p[1] = b; 965 break; 966 case ',': /* comma separated list */ 967 case '\0': 968 p[0] = p[1] = a; 969 break; 970 default: 971 warnx("port list: invalid separator <%c> in <%s>", 972 *s, av); 973 return (0); 974 } 975 976 i++; 977 p += 2; 978 av = s + 1; 979 } 980 if (i > 0) { 981 if (i + 1 > F_LEN_MASK) 982 errx(EX_DATAERR, "too many ports/ranges\n"); 983 cmd->o.len |= i + 1; /* leave F_NOT and F_OR untouched */ 984 } 985 return (i); 986 } 987 988 /* 989 * Fill the body of the command with the list of DiffServ codepoints. 990 */ 991 static void 992 fill_dscp(ipfw_insn *cmd, char *av, int cblen) 993 { 994 uint32_t *low, *high; 995 char *s = av, *a; 996 int code; 997 998 cmd->opcode = O_DSCP; 999 cmd->len |= F_INSN_SIZE(ipfw_insn_u32) + 1; 1000 1001 CHECK_CMDLEN; 1002 1003 low = (uint32_t *)(cmd + 1); 1004 high = low + 1; 1005 1006 *low = 0; 1007 *high = 0; 1008 1009 while (s != NULL) { 1010 a = strchr(s, ','); 1011 1012 if (a != NULL) 1013 *a++ = '\0'; 1014 1015 if (isalpha(*s)) { 1016 if ((code = match_token(f_ipdscp, s)) == -1) 1017 errx(EX_DATAERR, "Unknown DSCP code"); 1018 } else { 1019 code = strtoul(s, NULL, 10); 1020 if (code < 0 || code > 63) 1021 errx(EX_DATAERR, "Invalid DSCP value"); 1022 } 1023 1024 if (code > 32) 1025 *high |= 1 << (code - 32); 1026 else 1027 *low |= 1 << code; 1028 1029 s = a; 1030 } 1031 } 1032 1033 static struct _s_x icmpcodes[] = { 1034 { "net", ICMP_UNREACH_NET }, 1035 { "host", ICMP_UNREACH_HOST }, 1036 { "protocol", ICMP_UNREACH_PROTOCOL }, 1037 { "port", ICMP_UNREACH_PORT }, 1038 { "needfrag", ICMP_UNREACH_NEEDFRAG }, 1039 { "srcfail", ICMP_UNREACH_SRCFAIL }, 1040 { "net-unknown", ICMP_UNREACH_NET_UNKNOWN }, 1041 { "host-unknown", ICMP_UNREACH_HOST_UNKNOWN }, 1042 { "isolated", ICMP_UNREACH_ISOLATED }, 1043 { "net-prohib", ICMP_UNREACH_NET_PROHIB }, 1044 { "host-prohib", ICMP_UNREACH_HOST_PROHIB }, 1045 { "tosnet", ICMP_UNREACH_TOSNET }, 1046 { "toshost", ICMP_UNREACH_TOSHOST }, 1047 { "filter-prohib", ICMP_UNREACH_FILTER_PROHIB }, 1048 { "host-precedence", ICMP_UNREACH_HOST_PRECEDENCE }, 1049 { "precedence-cutoff", ICMP_UNREACH_PRECEDENCE_CUTOFF }, 1050 { NULL, 0 } 1051 }; 1052 1053 static void 1054 fill_reject_code(u_short *codep, char *str) 1055 { 1056 int val; 1057 char *s; 1058 1059 val = strtoul(str, &s, 0); 1060 if (s == str || *s != '\0' || val >= 0x100) 1061 val = match_token(icmpcodes, str); 1062 if (val < 0) 1063 errx(EX_DATAERR, "unknown ICMP unreachable code ``%s''", str); 1064 *codep = val; 1065 return; 1066 } 1067 1068 static void 1069 print_reject_code(struct buf_pr *bp, uint16_t code) 1070 { 1071 char const *s; 1072 1073 if ((s = match_value(icmpcodes, code)) != NULL) 1074 bprintf(bp, "unreach %s", s); 1075 else 1076 bprintf(bp, "unreach %u", code); 1077 } 1078 1079 /* 1080 * Returns the number of bits set (from left) in a contiguous bitmask, 1081 * or -1 if the mask is not contiguous. 1082 * XXX this needs a proper fix. 1083 * This effectively works on masks in big-endian (network) format. 1084 * when compiled on little endian architectures. 1085 * 1086 * First bit is bit 7 of the first byte -- note, for MAC addresses, 1087 * the first bit on the wire is bit 0 of the first byte. 1088 * len is the max length in bits. 1089 */ 1090 int 1091 contigmask(uint8_t *p, int len) 1092 { 1093 int i, n; 1094 1095 for (i=0; i<len ; i++) 1096 if ( (p[i/8] & (1 << (7 - (i%8)))) == 0) /* first bit unset */ 1097 break; 1098 for (n=i+1; n < len; n++) 1099 if ( (p[n/8] & (1 << (7 - (n%8)))) != 0) 1100 return -1; /* mask not contiguous */ 1101 return i; 1102 } 1103 1104 /* 1105 * print flags set/clear in the two bitmasks passed as parameters. 1106 * There is a specialized check for f_tcpflags. 1107 */ 1108 static void 1109 print_flags(struct buf_pr *bp, char const *name, ipfw_insn *cmd, 1110 struct _s_x *list) 1111 { 1112 char const *comma = ""; 1113 int i; 1114 uint8_t set = cmd->arg1 & 0xff; 1115 uint8_t clear = (cmd->arg1 >> 8) & 0xff; 1116 1117 if (list == f_tcpflags && set == TH_SYN && clear == TH_ACK) { 1118 bprintf(bp, " setup"); 1119 return; 1120 } 1121 1122 bprintf(bp, " %s ", name); 1123 for (i=0; list[i].x != 0; i++) { 1124 if (set & list[i].x) { 1125 set &= ~list[i].x; 1126 bprintf(bp, "%s%s", comma, list[i].s); 1127 comma = ","; 1128 } 1129 if (clear & list[i].x) { 1130 clear &= ~list[i].x; 1131 bprintf(bp, "%s!%s", comma, list[i].s); 1132 comma = ","; 1133 } 1134 } 1135 } 1136 1137 1138 /* 1139 * Print the ip address contained in a command. 1140 */ 1141 static void 1142 print_ip(struct buf_pr *bp, struct format_opts *fo, ipfw_insn_ip *cmd, 1143 char const *s) 1144 { 1145 struct hostent *he = NULL; 1146 struct in_addr *ia; 1147 uint32_t len = F_LEN((ipfw_insn *)cmd); 1148 uint32_t *a = ((ipfw_insn_u32 *)cmd)->d; 1149 char *t; 1150 1151 if (cmd->o.opcode == O_IP_DST_LOOKUP && len > F_INSN_SIZE(ipfw_insn_u32)) { 1152 uint32_t d = a[1]; 1153 const char *arg = "<invalid>"; 1154 1155 if (d < sizeof(lookup_key)/sizeof(lookup_key[0])) 1156 arg = match_value(rule_options, lookup_key[d]); 1157 t = table_search_ctlv(fo->tstate, ((ipfw_insn *)cmd)->arg1); 1158 bprintf(bp, "%s lookup %s %s", cmd->o.len & F_NOT ? " not": "", 1159 arg, t); 1160 return; 1161 } 1162 bprintf(bp, "%s%s ", cmd->o.len & F_NOT ? " not": "", s); 1163 1164 if (cmd->o.opcode == O_IP_SRC_ME || cmd->o.opcode == O_IP_DST_ME) { 1165 bprintf(bp, "me"); 1166 return; 1167 } 1168 if (cmd->o.opcode == O_IP_SRC_LOOKUP || 1169 cmd->o.opcode == O_IP_DST_LOOKUP) { 1170 t = table_search_ctlv(fo->tstate, ((ipfw_insn *)cmd)->arg1); 1171 bprintf(bp, "table(%s", t); 1172 if (len == F_INSN_SIZE(ipfw_insn_u32)) 1173 bprintf(bp, ",%u", *a); 1174 bprintf(bp, ")"); 1175 return; 1176 } 1177 if (cmd->o.opcode == O_IP_SRC_SET || cmd->o.opcode == O_IP_DST_SET) { 1178 uint32_t x, *map = (uint32_t *)&(cmd->mask); 1179 int i, j; 1180 char comma = '{'; 1181 1182 x = cmd->o.arg1 - 1; 1183 x = htonl( ~x ); 1184 cmd->addr.s_addr = htonl(cmd->addr.s_addr); 1185 bprintf(bp, "%s/%d", inet_ntoa(cmd->addr), 1186 contigmask((uint8_t *)&x, 32)); 1187 x = cmd->addr.s_addr = htonl(cmd->addr.s_addr); 1188 x &= 0xff; /* base */ 1189 /* 1190 * Print bits and ranges. 1191 * Locate first bit set (i), then locate first bit unset (j). 1192 * If we have 3+ consecutive bits set, then print them as a 1193 * range, otherwise only print the initial bit and rescan. 1194 */ 1195 for (i=0; i < cmd->o.arg1; i++) 1196 if (map[i/32] & (1<<(i & 31))) { 1197 for (j=i+1; j < cmd->o.arg1; j++) 1198 if (!(map[ j/32] & (1<<(j & 31)))) 1199 break; 1200 bprintf(bp, "%c%d", comma, i+x); 1201 if (j>i+2) { /* range has at least 3 elements */ 1202 bprintf(bp, "-%d", j-1+x); 1203 i = j-1; 1204 } 1205 comma = ','; 1206 } 1207 bprintf(bp, "}"); 1208 return; 1209 } 1210 /* 1211 * len == 2 indicates a single IP, whereas lists of 1 or more 1212 * addr/mask pairs have len = (2n+1). We convert len to n so we 1213 * use that to count the number of entries. 1214 */ 1215 for (len = len / 2; len > 0; len--, a += 2) { 1216 int mb = /* mask length */ 1217 (cmd->o.opcode == O_IP_SRC || cmd->o.opcode == O_IP_DST) ? 1218 32 : contigmask((uint8_t *)&(a[1]), 32); 1219 if (mb == 32 && co.do_resolv) 1220 he = gethostbyaddr((char *)&(a[0]), sizeof(u_long), AF_INET); 1221 if (he != NULL) /* resolved to name */ 1222 bprintf(bp, "%s", he->h_name); 1223 else if (mb == 0) /* any */ 1224 bprintf(bp, "any"); 1225 else { /* numeric IP followed by some kind of mask */ 1226 ia = (struct in_addr *)&a[0]; 1227 bprintf(bp, "%s", inet_ntoa(*ia)); 1228 if (mb < 0) 1229 bprintf(bp, ":%s", inet_ntoa(*ia ) ); 1230 else if (mb < 32) 1231 bprintf(bp, "/%d", mb); 1232 } 1233 if (len > 1) 1234 bprintf(bp, ","); 1235 } 1236 } 1237 1238 /* 1239 * prints a MAC address/mask pair 1240 */ 1241 static void 1242 print_mac(struct buf_pr *bp, uint8_t *addr, uint8_t *mask) 1243 { 1244 int l = contigmask(mask, 48); 1245 1246 if (l == 0) 1247 bprintf(bp, " any"); 1248 else { 1249 bprintf(bp, " %02x:%02x:%02x:%02x:%02x:%02x", 1250 addr[0], addr[1], addr[2], addr[3], addr[4], addr[5]); 1251 if (l == -1) 1252 bprintf(bp, "&%02x:%02x:%02x:%02x:%02x:%02x", 1253 mask[0], mask[1], mask[2], 1254 mask[3], mask[4], mask[5]); 1255 else if (l < 48) 1256 bprintf(bp, "/%d", l); 1257 } 1258 } 1259 1260 static void 1261 fill_icmptypes(ipfw_insn_u32 *cmd, char *av) 1262 { 1263 uint8_t type; 1264 1265 cmd->d[0] = 0; 1266 while (*av) { 1267 if (*av == ',') 1268 av++; 1269 1270 type = strtoul(av, &av, 0); 1271 1272 if (*av != ',' && *av != '\0') 1273 errx(EX_DATAERR, "invalid ICMP type"); 1274 1275 if (type > 31) 1276 errx(EX_DATAERR, "ICMP type out of range"); 1277 1278 cmd->d[0] |= 1 << type; 1279 } 1280 cmd->o.opcode = O_ICMPTYPE; 1281 cmd->o.len |= F_INSN_SIZE(ipfw_insn_u32); 1282 } 1283 1284 static void 1285 print_icmptypes(struct buf_pr *bp, ipfw_insn_u32 *cmd) 1286 { 1287 int i; 1288 char sep= ' '; 1289 1290 bprintf(bp, " icmptypes"); 1291 for (i = 0; i < 32; i++) { 1292 if ( (cmd->d[0] & (1 << (i))) == 0) 1293 continue; 1294 bprintf(bp, "%c%d", sep, i); 1295 sep = ','; 1296 } 1297 } 1298 1299 static void 1300 print_dscp(struct buf_pr *bp, ipfw_insn_u32 *cmd) 1301 { 1302 int i, c; 1303 uint32_t *v; 1304 char sep= ' '; 1305 const char *code; 1306 1307 bprintf(bp, " dscp"); 1308 i = 0; 1309 c = 0; 1310 v = cmd->d; 1311 while (i < 64) { 1312 if (*v & (1 << i)) { 1313 if ((code = match_value(f_ipdscp, i)) != NULL) 1314 bprintf(bp, "%c%s", sep, code); 1315 else 1316 bprintf(bp, "%c%d", sep, i); 1317 sep = ','; 1318 } 1319 1320 if ((++i % 32) == 0) 1321 v++; 1322 } 1323 } 1324 1325 /* 1326 * show_ipfw() prints the body of an ipfw rule. 1327 * Because the standard rule has at least proto src_ip dst_ip, we use 1328 * a helper function to produce these entries if not provided explicitly. 1329 * The first argument is the list of fields we have, the second is 1330 * the list of fields we want to be printed. 1331 * 1332 * Special cases if we have provided a MAC header: 1333 * + if the rule does not contain IP addresses/ports, do not print them; 1334 * + if the rule does not contain an IP proto, print "all" instead of "ip"; 1335 * 1336 * Once we have 'have_options', IP header fields are printed as options. 1337 */ 1338 #define HAVE_PROTO 0x0001 1339 #define HAVE_SRCIP 0x0002 1340 #define HAVE_DSTIP 0x0004 1341 #define HAVE_PROTO4 0x0008 1342 #define HAVE_PROTO6 0x0010 1343 #define HAVE_IP 0x0100 1344 #define HAVE_OPTIONS 0x8000 1345 1346 static void 1347 show_prerequisites(struct buf_pr *bp, int *flags, int want, int cmd) 1348 { 1349 (void)cmd; /* UNUSED */ 1350 if (co.comment_only) 1351 return; 1352 if ( (*flags & HAVE_IP) == HAVE_IP) 1353 *flags |= HAVE_OPTIONS; 1354 1355 if ( !(*flags & HAVE_OPTIONS)) { 1356 if ( !(*flags & HAVE_PROTO) && (want & HAVE_PROTO)) { 1357 if ( (*flags & HAVE_PROTO4)) 1358 bprintf(bp, " ip4"); 1359 else if ( (*flags & HAVE_PROTO6)) 1360 bprintf(bp, " ip6"); 1361 else 1362 bprintf(bp, " ip"); 1363 } 1364 if ( !(*flags & HAVE_SRCIP) && (want & HAVE_SRCIP)) 1365 bprintf(bp, " from any"); 1366 if ( !(*flags & HAVE_DSTIP) && (want & HAVE_DSTIP)) 1367 bprintf(bp, " to any"); 1368 } 1369 *flags |= want; 1370 } 1371 1372 static void 1373 show_static_rule(struct cmdline_opts *co, struct format_opts *fo, 1374 struct buf_pr *bp, struct ip_fw_rule *rule, struct ip_fw_bcounter *cntr) 1375 { 1376 static int twidth = 0; 1377 int l; 1378 ipfw_insn *cmd, *tagptr = NULL; 1379 const char *comment = NULL; /* ptr to comment if we have one */ 1380 int proto = 0; /* default */ 1381 int flags = 0; /* prerequisites */ 1382 ipfw_insn_log *logptr = NULL; /* set if we find an O_LOG */ 1383 ipfw_insn_altq *altqptr = NULL; /* set if we find an O_ALTQ */ 1384 int or_block = 0; /* we are in an or block */ 1385 uint32_t uval; 1386 1387 if ((fo->set_mask & (1 << rule->set)) == 0) { 1388 /* disabled mask */ 1389 if (!co->show_sets) 1390 return; 1391 else 1392 bprintf(bp, "# DISABLED "); 1393 } 1394 bprintf(bp, "%05u ", rule->rulenum); 1395 1396 /* Print counters if enabled */ 1397 if (fo->pcwidth > 0 || fo->bcwidth > 0) { 1398 pr_u64(bp, &cntr->pcnt, fo->pcwidth); 1399 pr_u64(bp, &cntr->bcnt, fo->bcwidth); 1400 } 1401 1402 if (co->do_time == 2) 1403 bprintf(bp, "%10u ", cntr->timestamp); 1404 else if (co->do_time == 1) { 1405 char timestr[30]; 1406 time_t t = (time_t)0; 1407 1408 if (twidth == 0) { 1409 strcpy(timestr, ctime(&t)); 1410 *strchr(timestr, '\n') = '\0'; 1411 twidth = strlen(timestr); 1412 } 1413 if (cntr->timestamp > 0) { 1414 t = _long_to_time(cntr->timestamp); 1415 1416 strcpy(timestr, ctime(&t)); 1417 *strchr(timestr, '\n') = '\0'; 1418 bprintf(bp, "%s ", timestr); 1419 } else { 1420 bprintf(bp, "%*s", twidth, " "); 1421 } 1422 } 1423 1424 if (co->show_sets) 1425 bprintf(bp, "set %d ", rule->set); 1426 1427 /* 1428 * print the optional "match probability" 1429 */ 1430 if (rule->cmd_len > 0) { 1431 cmd = rule->cmd ; 1432 if (cmd->opcode == O_PROB) { 1433 ipfw_insn_u32 *p = (ipfw_insn_u32 *)cmd; 1434 double d = 1.0 * p->d[0]; 1435 1436 d = (d / 0x7fffffff); 1437 bprintf(bp, "prob %f ", d); 1438 } 1439 } 1440 1441 /* 1442 * first print actions 1443 */ 1444 for (l = rule->cmd_len - rule->act_ofs, cmd = ACTION_PTR(rule); 1445 l > 0 ; l -= F_LEN(cmd), cmd += F_LEN(cmd)) { 1446 switch(cmd->opcode) { 1447 case O_CHECK_STATE: 1448 bprintf(bp, "check-state"); 1449 /* avoid printing anything else */ 1450 flags = HAVE_PROTO | HAVE_SRCIP | 1451 HAVE_DSTIP | HAVE_IP; 1452 break; 1453 1454 case O_ACCEPT: 1455 bprintf(bp, "allow"); 1456 break; 1457 1458 case O_COUNT: 1459 bprintf(bp, "count"); 1460 break; 1461 1462 case O_DENY: 1463 bprintf(bp, "deny"); 1464 break; 1465 1466 case O_REJECT: 1467 if (cmd->arg1 == ICMP_REJECT_RST) 1468 bprintf(bp, "reset"); 1469 else if (cmd->arg1 == ICMP_UNREACH_HOST) 1470 bprintf(bp, "reject"); 1471 else 1472 print_reject_code(bp, cmd->arg1); 1473 break; 1474 1475 case O_UNREACH6: 1476 if (cmd->arg1 == ICMP6_UNREACH_RST) 1477 bprintf(bp, "reset6"); 1478 else 1479 print_unreach6_code(cmd->arg1); 1480 break; 1481 1482 case O_SKIPTO: 1483 bprint_uint_arg(bp, "skipto ", cmd->arg1); 1484 break; 1485 1486 case O_PIPE: 1487 bprint_uint_arg(bp, "pipe ", cmd->arg1); 1488 break; 1489 1490 case O_QUEUE: 1491 bprint_uint_arg(bp, "queue ", cmd->arg1); 1492 break; 1493 1494 case O_DIVERT: 1495 bprint_uint_arg(bp, "divert ", cmd->arg1); 1496 break; 1497 1498 case O_TEE: 1499 bprint_uint_arg(bp, "tee ", cmd->arg1); 1500 break; 1501 1502 case O_NETGRAPH: 1503 bprint_uint_arg(bp, "netgraph ", cmd->arg1); 1504 break; 1505 1506 case O_NGTEE: 1507 bprint_uint_arg(bp, "ngtee ", cmd->arg1); 1508 break; 1509 1510 case O_FORWARD_IP: 1511 { 1512 ipfw_insn_sa *s = (ipfw_insn_sa *)cmd; 1513 1514 if (s->sa.sin_addr.s_addr == INADDR_ANY) { 1515 bprintf(bp, "fwd tablearg"); 1516 } else { 1517 bprintf(bp, "fwd %s",inet_ntoa(s->sa.sin_addr)); 1518 } 1519 if (s->sa.sin_port) 1520 bprintf(bp, ",%d", s->sa.sin_port); 1521 } 1522 break; 1523 1524 case O_FORWARD_IP6: 1525 { 1526 char buf[4 + INET6_ADDRSTRLEN + 1]; 1527 ipfw_insn_sa6 *s = (ipfw_insn_sa6 *)cmd; 1528 1529 bprintf(bp, "fwd %s", inet_ntop(AF_INET6, 1530 &s->sa.sin6_addr, buf, sizeof(buf))); 1531 if (s->sa.sin6_port) 1532 bprintf(bp, ",%d", s->sa.sin6_port); 1533 } 1534 break; 1535 1536 case O_LOG: /* O_LOG is printed last */ 1537 logptr = (ipfw_insn_log *)cmd; 1538 break; 1539 1540 case O_ALTQ: /* O_ALTQ is printed after O_LOG */ 1541 altqptr = (ipfw_insn_altq *)cmd; 1542 break; 1543 1544 case O_TAG: 1545 tagptr = cmd; 1546 break; 1547 1548 case O_NAT: 1549 if (cmd->arg1 != 0) 1550 bprint_uint_arg(bp, "nat ", cmd->arg1); 1551 else 1552 bprintf(bp, "nat global"); 1553 break; 1554 1555 case O_SETFIB: 1556 bprint_uint_arg(bp, "setfib ", cmd->arg1 & 0x7FFF); 1557 break; 1558 1559 case O_SETDSCP: 1560 { 1561 const char *code; 1562 1563 if (cmd->arg1 == IP_FW_TARG) { 1564 bprint_uint_arg(bp, "setdscp ", cmd->arg1); 1565 break; 1566 } 1567 uval = cmd->arg1 & 0x3F; 1568 if ((code = match_value(f_ipdscp, uval)) != NULL) 1569 bprintf(bp, "setdscp %s", code); 1570 else 1571 bprint_uint_arg(bp, "setdscp ", uval); 1572 } 1573 break; 1574 1575 case O_REASS: 1576 bprintf(bp, "reass"); 1577 break; 1578 1579 case O_CALLRETURN: 1580 if (cmd->len & F_NOT) 1581 bprintf(bp, "return"); 1582 else 1583 bprint_uint_arg(bp, "call ", cmd->arg1); 1584 break; 1585 1586 default: 1587 bprintf(bp, "** unrecognized action %d len %d ", 1588 cmd->opcode, cmd->len); 1589 } 1590 } 1591 if (logptr) { 1592 if (logptr->max_log > 0) 1593 bprintf(bp, " log logamount %d", logptr->max_log); 1594 else 1595 bprintf(bp, " log"); 1596 } 1597 #ifndef NO_ALTQ 1598 if (altqptr) { 1599 print_altq_cmd(bp, altqptr); 1600 } 1601 #endif 1602 if (tagptr) { 1603 if (tagptr->len & F_NOT) 1604 bprint_uint_arg(bp, " untag ", tagptr->arg1); 1605 else 1606 bprint_uint_arg(bp, " tag ", tagptr->arg1); 1607 } 1608 1609 /* 1610 * then print the body. 1611 */ 1612 for (l = rule->act_ofs, cmd = rule->cmd; 1613 l > 0 ; l -= F_LEN(cmd) , cmd += F_LEN(cmd)) { 1614 if ((cmd->len & F_OR) || (cmd->len & F_NOT)) 1615 continue; 1616 if (cmd->opcode == O_IP4) { 1617 flags |= HAVE_PROTO4; 1618 break; 1619 } else if (cmd->opcode == O_IP6) { 1620 flags |= HAVE_PROTO6; 1621 break; 1622 } 1623 } 1624 if (rule->flags & IPFW_RULE_NOOPT) { /* empty rules before options */ 1625 if (!co->do_compact) { 1626 show_prerequisites(bp, &flags, HAVE_PROTO, 0); 1627 bprintf(bp, " from any to any"); 1628 } 1629 flags |= HAVE_IP | HAVE_OPTIONS | HAVE_PROTO | 1630 HAVE_SRCIP | HAVE_DSTIP; 1631 } 1632 1633 if (co->comment_only) 1634 comment = "..."; 1635 1636 for (l = rule->act_ofs, cmd = rule->cmd; 1637 l > 0 ; l -= F_LEN(cmd) , cmd += F_LEN(cmd)) { 1638 /* useful alias */ 1639 ipfw_insn_u32 *cmd32 = (ipfw_insn_u32 *)cmd; 1640 1641 if (co->comment_only) { 1642 if (cmd->opcode != O_NOP) 1643 continue; 1644 bprintf(bp, " // %s\n", (char *)(cmd + 1)); 1645 return; 1646 } 1647 1648 show_prerequisites(bp, &flags, 0, cmd->opcode); 1649 1650 switch(cmd->opcode) { 1651 case O_PROB: 1652 break; /* done already */ 1653 1654 case O_PROBE_STATE: 1655 break; /* no need to print anything here */ 1656 1657 case O_IP_SRC: 1658 case O_IP_SRC_LOOKUP: 1659 case O_IP_SRC_MASK: 1660 case O_IP_SRC_ME: 1661 case O_IP_SRC_SET: 1662 show_prerequisites(bp, &flags, HAVE_PROTO, 0); 1663 if (!(flags & HAVE_SRCIP)) 1664 bprintf(bp, " from"); 1665 if ((cmd->len & F_OR) && !or_block) 1666 bprintf(bp, " {"); 1667 print_ip(bp, fo, (ipfw_insn_ip *)cmd, 1668 (flags & HAVE_OPTIONS) ? " src-ip" : ""); 1669 flags |= HAVE_SRCIP; 1670 break; 1671 1672 case O_IP_DST: 1673 case O_IP_DST_LOOKUP: 1674 case O_IP_DST_MASK: 1675 case O_IP_DST_ME: 1676 case O_IP_DST_SET: 1677 show_prerequisites(bp, &flags, HAVE_PROTO|HAVE_SRCIP, 0); 1678 if (!(flags & HAVE_DSTIP)) 1679 bprintf(bp, " to"); 1680 if ((cmd->len & F_OR) && !or_block) 1681 bprintf(bp, " {"); 1682 print_ip(bp, fo, (ipfw_insn_ip *)cmd, 1683 (flags & HAVE_OPTIONS) ? " dst-ip" : ""); 1684 flags |= HAVE_DSTIP; 1685 break; 1686 1687 case O_IP6_SRC: 1688 case O_IP6_SRC_MASK: 1689 case O_IP6_SRC_ME: 1690 show_prerequisites(bp, &flags, HAVE_PROTO, 0); 1691 if (!(flags & HAVE_SRCIP)) 1692 bprintf(bp, " from"); 1693 if ((cmd->len & F_OR) && !or_block) 1694 bprintf(bp, " {"); 1695 print_ip6(bp, (ipfw_insn_ip6 *)cmd, 1696 (flags & HAVE_OPTIONS) ? " src-ip6" : ""); 1697 flags |= HAVE_SRCIP | HAVE_PROTO; 1698 break; 1699 1700 case O_IP6_DST: 1701 case O_IP6_DST_MASK: 1702 case O_IP6_DST_ME: 1703 show_prerequisites(bp, &flags, HAVE_PROTO|HAVE_SRCIP, 0); 1704 if (!(flags & HAVE_DSTIP)) 1705 bprintf(bp, " to"); 1706 if ((cmd->len & F_OR) && !or_block) 1707 bprintf(bp, " {"); 1708 print_ip6(bp, (ipfw_insn_ip6 *)cmd, 1709 (flags & HAVE_OPTIONS) ? " dst-ip6" : ""); 1710 flags |= HAVE_DSTIP; 1711 break; 1712 1713 case O_FLOW6ID: 1714 print_flow6id(bp, (ipfw_insn_u32 *) cmd ); 1715 flags |= HAVE_OPTIONS; 1716 break; 1717 1718 case O_IP_DSTPORT: 1719 show_prerequisites(bp, &flags, 1720 HAVE_PROTO | HAVE_SRCIP | 1721 HAVE_DSTIP | HAVE_IP, 0); 1722 case O_IP_SRCPORT: 1723 if (flags & HAVE_DSTIP) 1724 flags |= HAVE_IP; 1725 show_prerequisites(bp, &flags, 1726 HAVE_PROTO | HAVE_SRCIP, 0); 1727 if ((cmd->len & F_OR) && !or_block) 1728 bprintf(bp, " {"); 1729 if (cmd->len & F_NOT) 1730 bprintf(bp, " not"); 1731 print_newports(bp, (ipfw_insn_u16 *)cmd, proto, 1732 (flags & HAVE_OPTIONS) ? cmd->opcode : 0); 1733 break; 1734 1735 case O_PROTO: { 1736 struct protoent *pe = NULL; 1737 1738 if ((cmd->len & F_OR) && !or_block) 1739 bprintf(bp, " {"); 1740 if (cmd->len & F_NOT) 1741 bprintf(bp, " not"); 1742 proto = cmd->arg1; 1743 pe = getprotobynumber(cmd->arg1); 1744 if ((flags & (HAVE_PROTO4 | HAVE_PROTO6)) && 1745 !(flags & HAVE_PROTO)) 1746 show_prerequisites(bp, &flags, 1747 HAVE_PROTO | HAVE_IP | HAVE_SRCIP | 1748 HAVE_DSTIP | HAVE_OPTIONS, 0); 1749 if (flags & HAVE_OPTIONS) 1750 bprintf(bp, " proto"); 1751 if (pe) 1752 bprintf(bp, " %s", pe->p_name); 1753 else 1754 bprintf(bp, " %u", cmd->arg1); 1755 } 1756 flags |= HAVE_PROTO; 1757 break; 1758 1759 default: /*options ... */ 1760 if (!(cmd->len & (F_OR|F_NOT))) 1761 if (((cmd->opcode == O_IP6) && 1762 (flags & HAVE_PROTO6)) || 1763 ((cmd->opcode == O_IP4) && 1764 (flags & HAVE_PROTO4))) 1765 break; 1766 show_prerequisites(bp, &flags, HAVE_PROTO | HAVE_SRCIP | 1767 HAVE_DSTIP | HAVE_IP | HAVE_OPTIONS, 0); 1768 if ((cmd->len & F_OR) && !or_block) 1769 bprintf(bp, " {"); 1770 if (cmd->len & F_NOT && cmd->opcode != O_IN) 1771 bprintf(bp, " not"); 1772 switch(cmd->opcode) { 1773 case O_MACADDR2: { 1774 ipfw_insn_mac *m = (ipfw_insn_mac *)cmd; 1775 1776 bprintf(bp, " MAC"); 1777 print_mac(bp, m->addr, m->mask); 1778 print_mac(bp, m->addr + 6, m->mask + 6); 1779 } 1780 break; 1781 1782 case O_MAC_TYPE: 1783 print_newports(bp, (ipfw_insn_u16 *)cmd, 1784 IPPROTO_ETHERTYPE, cmd->opcode); 1785 break; 1786 1787 1788 case O_FRAG: 1789 bprintf(bp, " frag"); 1790 break; 1791 1792 case O_FIB: 1793 bprintf(bp, " fib %u", cmd->arg1 ); 1794 break; 1795 case O_SOCKARG: 1796 bprintf(bp, " sockarg"); 1797 break; 1798 1799 case O_IN: 1800 bprintf(bp, cmd->len & F_NOT ? " out" : " in"); 1801 break; 1802 1803 case O_DIVERTED: 1804 switch (cmd->arg1) { 1805 case 3: 1806 bprintf(bp, " diverted"); 1807 break; 1808 case 1: 1809 bprintf(bp, " diverted-loopback"); 1810 break; 1811 case 2: 1812 bprintf(bp, " diverted-output"); 1813 break; 1814 default: 1815 bprintf(bp, " diverted-?<%u>", cmd->arg1); 1816 break; 1817 } 1818 break; 1819 1820 case O_LAYER2: 1821 bprintf(bp, " layer2"); 1822 break; 1823 case O_XMIT: 1824 case O_RECV: 1825 case O_VIA: 1826 { 1827 char const *s, *t; 1828 ipfw_insn_if *cmdif = (ipfw_insn_if *)cmd; 1829 1830 if (cmd->opcode == O_XMIT) 1831 s = "xmit"; 1832 else if (cmd->opcode == O_RECV) 1833 s = "recv"; 1834 else /* if (cmd->opcode == O_VIA) */ 1835 s = "via"; 1836 if (cmdif->name[0] == '\0') 1837 bprintf(bp, " %s %s", s, 1838 inet_ntoa(cmdif->p.ip)); 1839 else if (cmdif->name[0] == '\1') { 1840 /* interface table */ 1841 t = table_search_ctlv(fo->tstate, 1842 cmdif->p.kidx); 1843 bprintf(bp, " %s table(%s)", s, t); 1844 } else 1845 bprintf(bp, " %s %s", s, cmdif->name); 1846 1847 break; 1848 } 1849 case O_IP_FLOW_LOOKUP: 1850 { 1851 char *t; 1852 1853 t = table_search_ctlv(fo->tstate, cmd->arg1); 1854 bprintf(bp, " flow table(%s", t); 1855 if (F_LEN(cmd) == F_INSN_SIZE(ipfw_insn_u32)) 1856 bprintf(bp, ",%u", 1857 ((ipfw_insn_u32 *)cmd)->d[0]); 1858 bprintf(bp, ")"); 1859 break; 1860 } 1861 case O_IPID: 1862 if (F_LEN(cmd) == 1) 1863 bprintf(bp, " ipid %u", cmd->arg1 ); 1864 else 1865 print_newports(bp, (ipfw_insn_u16 *)cmd, 0, 1866 O_IPID); 1867 break; 1868 1869 case O_IPTTL: 1870 if (F_LEN(cmd) == 1) 1871 bprintf(bp, " ipttl %u", cmd->arg1 ); 1872 else 1873 print_newports(bp, (ipfw_insn_u16 *)cmd, 0, 1874 O_IPTTL); 1875 break; 1876 1877 case O_IPVER: 1878 bprintf(bp, " ipver %u", cmd->arg1 ); 1879 break; 1880 1881 case O_IPPRECEDENCE: 1882 bprintf(bp, " ipprecedence %u", cmd->arg1 >> 5); 1883 break; 1884 1885 case O_DSCP: 1886 print_dscp(bp, (ipfw_insn_u32 *)cmd); 1887 break; 1888 1889 case O_IPLEN: 1890 if (F_LEN(cmd) == 1) 1891 bprintf(bp, " iplen %u", cmd->arg1 ); 1892 else 1893 print_newports(bp, (ipfw_insn_u16 *)cmd, 0, 1894 O_IPLEN); 1895 break; 1896 1897 case O_IPOPT: 1898 print_flags(bp, "ipoptions", cmd, f_ipopts); 1899 break; 1900 1901 case O_IPTOS: 1902 print_flags(bp, "iptos", cmd, f_iptos); 1903 break; 1904 1905 case O_ICMPTYPE: 1906 print_icmptypes(bp, (ipfw_insn_u32 *)cmd); 1907 break; 1908 1909 case O_ESTAB: 1910 bprintf(bp, " established"); 1911 break; 1912 1913 case O_TCPDATALEN: 1914 if (F_LEN(cmd) == 1) 1915 bprintf(bp, " tcpdatalen %u", cmd->arg1 ); 1916 else 1917 print_newports(bp, (ipfw_insn_u16 *)cmd, 0, 1918 O_TCPDATALEN); 1919 break; 1920 1921 case O_TCPFLAGS: 1922 print_flags(bp, "tcpflags", cmd, f_tcpflags); 1923 break; 1924 1925 case O_TCPOPTS: 1926 print_flags(bp, "tcpoptions", cmd, f_tcpopts); 1927 break; 1928 1929 case O_TCPWIN: 1930 if (F_LEN(cmd) == 1) 1931 bprintf(bp, " tcpwin %u", cmd->arg1); 1932 else 1933 print_newports(bp, (ipfw_insn_u16 *)cmd, 0, 1934 O_TCPWIN); 1935 break; 1936 1937 case O_TCPACK: 1938 bprintf(bp, " tcpack %d", ntohl(cmd32->d[0])); 1939 break; 1940 1941 case O_TCPSEQ: 1942 bprintf(bp, " tcpseq %d", ntohl(cmd32->d[0])); 1943 break; 1944 1945 case O_UID: 1946 { 1947 struct passwd *pwd = getpwuid(cmd32->d[0]); 1948 1949 if (pwd) 1950 bprintf(bp, " uid %s", pwd->pw_name); 1951 else 1952 bprintf(bp, " uid %u", cmd32->d[0]); 1953 } 1954 break; 1955 1956 case O_GID: 1957 { 1958 struct group *grp = getgrgid(cmd32->d[0]); 1959 1960 if (grp) 1961 bprintf(bp, " gid %s", grp->gr_name); 1962 else 1963 bprintf(bp, " gid %u", cmd32->d[0]); 1964 } 1965 break; 1966 1967 case O_JAIL: 1968 bprintf(bp, " jail %d", cmd32->d[0]); 1969 break; 1970 1971 case O_VERREVPATH: 1972 bprintf(bp, " verrevpath"); 1973 break; 1974 1975 case O_VERSRCREACH: 1976 bprintf(bp, " versrcreach"); 1977 break; 1978 1979 case O_ANTISPOOF: 1980 bprintf(bp, " antispoof"); 1981 break; 1982 1983 case O_IPSEC: 1984 bprintf(bp, " ipsec"); 1985 break; 1986 1987 case O_NOP: 1988 comment = (char *)(cmd + 1); 1989 break; 1990 1991 case O_KEEP_STATE: 1992 bprintf(bp, " keep-state"); 1993 break; 1994 1995 case O_LIMIT: { 1996 struct _s_x *p = limit_masks; 1997 ipfw_insn_limit *c = (ipfw_insn_limit *)cmd; 1998 uint8_t x = c->limit_mask; 1999 char const *comma = " "; 2000 2001 bprintf(bp, " limit"); 2002 for (; p->x != 0 ; p++) 2003 if ((x & p->x) == p->x) { 2004 x &= ~p->x; 2005 bprintf(bp, "%s%s", comma,p->s); 2006 comma = ","; 2007 } 2008 bprint_uint_arg(bp, " ", c->conn_limit); 2009 break; 2010 } 2011 2012 case O_IP6: 2013 bprintf(bp, " ip6"); 2014 break; 2015 2016 case O_IP4: 2017 bprintf(bp, " ip4"); 2018 break; 2019 2020 case O_ICMP6TYPE: 2021 print_icmp6types(bp, (ipfw_insn_u32 *)cmd); 2022 break; 2023 2024 case O_EXT_HDR: 2025 print_ext6hdr(bp, (ipfw_insn *)cmd); 2026 break; 2027 2028 case O_TAGGED: 2029 if (F_LEN(cmd) == 1) 2030 bprint_uint_arg(bp, " tagged ", 2031 cmd->arg1); 2032 else 2033 print_newports(bp, (ipfw_insn_u16 *)cmd, 2034 0, O_TAGGED); 2035 break; 2036 2037 default: 2038 bprintf(bp, " [opcode %d len %d]", 2039 cmd->opcode, cmd->len); 2040 } 2041 } 2042 if (cmd->len & F_OR) { 2043 bprintf(bp, " or"); 2044 or_block = 1; 2045 } else if (or_block) { 2046 bprintf(bp, " }"); 2047 or_block = 0; 2048 } 2049 } 2050 show_prerequisites(bp, &flags, HAVE_PROTO | HAVE_SRCIP | HAVE_DSTIP 2051 | HAVE_IP, 0); 2052 if (comment) 2053 bprintf(bp, " // %s", comment); 2054 bprintf(bp, "\n"); 2055 } 2056 2057 static void 2058 show_dyn_state(struct cmdline_opts *co, struct format_opts *fo, 2059 struct buf_pr *bp, ipfw_dyn_rule *d) 2060 { 2061 struct protoent *pe; 2062 struct in_addr a; 2063 uint16_t rulenum; 2064 char buf[INET6_ADDRSTRLEN]; 2065 2066 if (!co->do_expired) { 2067 if (!d->expire && !(d->dyn_type == O_LIMIT_PARENT)) 2068 return; 2069 } 2070 bcopy(&d->rule, &rulenum, sizeof(rulenum)); 2071 bprintf(bp, "%05d", rulenum); 2072 if (fo->pcwidth > 0 || fo->bcwidth > 0) { 2073 bprintf(bp, " "); 2074 pr_u64(bp, &d->pcnt, fo->pcwidth); 2075 pr_u64(bp, &d->bcnt, fo->bcwidth); 2076 bprintf(bp, "(%ds)", d->expire); 2077 } 2078 switch (d->dyn_type) { 2079 case O_LIMIT_PARENT: 2080 bprintf(bp, " PARENT %d", d->count); 2081 break; 2082 case O_LIMIT: 2083 bprintf(bp, " LIMIT"); 2084 break; 2085 case O_KEEP_STATE: /* bidir, no mask */ 2086 bprintf(bp, " STATE"); 2087 break; 2088 } 2089 2090 if ((pe = getprotobynumber(d->id.proto)) != NULL) 2091 bprintf(bp, " %s", pe->p_name); 2092 else 2093 bprintf(bp, " proto %u", d->id.proto); 2094 2095 if (d->id.addr_type == 4) { 2096 a.s_addr = htonl(d->id.src_ip); 2097 bprintf(bp, " %s %d", inet_ntoa(a), d->id.src_port); 2098 2099 a.s_addr = htonl(d->id.dst_ip); 2100 bprintf(bp, " <-> %s %d", inet_ntoa(a), d->id.dst_port); 2101 } else if (d->id.addr_type == 6) { 2102 bprintf(bp, " %s %d", inet_ntop(AF_INET6, &d->id.src_ip6, buf, 2103 sizeof(buf)), d->id.src_port); 2104 bprintf(bp, " <-> %s %d", inet_ntop(AF_INET6, &d->id.dst_ip6, 2105 buf, sizeof(buf)), d->id.dst_port); 2106 } else 2107 bprintf(bp, " UNKNOWN <-> UNKNOWN\n"); 2108 } 2109 2110 static int 2111 do_range_cmd(int cmd, ipfw_range_tlv *rt) 2112 { 2113 ipfw_range_header rh; 2114 size_t sz; 2115 2116 memset(&rh, 0, sizeof(rh)); 2117 memcpy(&rh.range, rt, sizeof(*rt)); 2118 rh.range.head.length = sizeof(*rt); 2119 rh.range.head.type = IPFW_TLV_RANGE; 2120 sz = sizeof(rh); 2121 2122 if (do_get3(cmd, &rh.opheader, &sz) != 0) 2123 return (-1); 2124 /* Save number of matched objects */ 2125 rt->new_set = rh.range.new_set; 2126 return (0); 2127 } 2128 2129 /* 2130 * This one handles all set-related commands 2131 * ipfw set { show | enable | disable } 2132 * ipfw set swap X Y 2133 * ipfw set move X to Y 2134 * ipfw set move rule X to Y 2135 */ 2136 void 2137 ipfw_sets_handler(char *av[]) 2138 { 2139 uint32_t masks[2]; 2140 int i; 2141 uint8_t cmd, new_set, rulenum; 2142 ipfw_range_tlv rt; 2143 char *msg; 2144 size_t size; 2145 2146 av++; 2147 memset(&rt, 0, sizeof(rt)); 2148 2149 if (av[0] == NULL) 2150 errx(EX_USAGE, "set needs command"); 2151 if (_substrcmp(*av, "show") == 0) { 2152 struct format_opts fo; 2153 ipfw_cfg_lheader *cfg; 2154 2155 memset(&fo, 0, sizeof(fo)); 2156 if (ipfw_get_config(&co, &fo, &cfg, &size) != 0) 2157 err(EX_OSERR, "requesting config failed"); 2158 2159 for (i = 0, msg = "disable"; i < RESVD_SET; i++) 2160 if ((cfg->set_mask & (1<<i)) == 0) { 2161 printf("%s %d", msg, i); 2162 msg = ""; 2163 } 2164 msg = (cfg->set_mask != (uint32_t)-1) ? " enable" : "enable"; 2165 for (i = 0; i < RESVD_SET; i++) 2166 if ((cfg->set_mask & (1<<i)) != 0) { 2167 printf("%s %d", msg, i); 2168 msg = ""; 2169 } 2170 printf("\n"); 2171 free(cfg); 2172 } else if (_substrcmp(*av, "swap") == 0) { 2173 av++; 2174 if ( av[0] == NULL || av[1] == NULL ) 2175 errx(EX_USAGE, "set swap needs 2 set numbers\n"); 2176 rt.set = atoi(av[0]); 2177 rt.new_set = atoi(av[1]); 2178 if (!isdigit(*(av[0])) || rt.set > RESVD_SET) 2179 errx(EX_DATAERR, "invalid set number %s\n", av[0]); 2180 if (!isdigit(*(av[1])) || rt.new_set > RESVD_SET) 2181 errx(EX_DATAERR, "invalid set number %s\n", av[1]); 2182 i = do_range_cmd(IP_FW_SET_SWAP, &rt); 2183 } else if (_substrcmp(*av, "move") == 0) { 2184 av++; 2185 if (av[0] && _substrcmp(*av, "rule") == 0) { 2186 rt.flags = IPFW_RCFLAG_RANGE; /* move rules to new set */ 2187 cmd = IP_FW_XMOVE; 2188 av++; 2189 } else 2190 cmd = IP_FW_SET_MOVE; /* Move set to new one */ 2191 if (av[0] == NULL || av[1] == NULL || av[2] == NULL || 2192 av[3] != NULL || _substrcmp(av[1], "to") != 0) 2193 errx(EX_USAGE, "syntax: set move [rule] X to Y\n"); 2194 rulenum = atoi(av[0]); 2195 rt.new_set = atoi(av[2]); 2196 if (cmd == IP_FW_XMOVE) { 2197 rt.start_rule = rulenum; 2198 rt.end_rule = rulenum; 2199 } else 2200 rt.set = rulenum; 2201 rt.new_set = atoi(av[2]); 2202 if (!isdigit(*(av[0])) || (cmd == 3 && rt.set > RESVD_SET) || 2203 (cmd == 2 && rt.start_rule == IPFW_DEFAULT_RULE) ) 2204 errx(EX_DATAERR, "invalid source number %s\n", av[0]); 2205 if (!isdigit(*(av[2])) || new_set > RESVD_SET) 2206 errx(EX_DATAERR, "invalid dest. set %s\n", av[1]); 2207 i = do_range_cmd(cmd, &rt); 2208 } else if (_substrcmp(*av, "disable") == 0 || 2209 _substrcmp(*av, "enable") == 0 ) { 2210 int which = _substrcmp(*av, "enable") == 0 ? 1 : 0; 2211 2212 av++; 2213 masks[0] = masks[1] = 0; 2214 2215 while (av[0]) { 2216 if (isdigit(**av)) { 2217 i = atoi(*av); 2218 if (i < 0 || i > RESVD_SET) 2219 errx(EX_DATAERR, 2220 "invalid set number %d\n", i); 2221 masks[which] |= (1<<i); 2222 } else if (_substrcmp(*av, "disable") == 0) 2223 which = 0; 2224 else if (_substrcmp(*av, "enable") == 0) 2225 which = 1; 2226 else 2227 errx(EX_DATAERR, 2228 "invalid set command %s\n", *av); 2229 av++; 2230 } 2231 if ( (masks[0] & masks[1]) != 0 ) 2232 errx(EX_DATAERR, 2233 "cannot enable and disable the same set\n"); 2234 2235 rt.set = masks[0]; 2236 rt.new_set = masks[1]; 2237 i = do_range_cmd(IP_FW_SET_ENABLE, &rt); 2238 if (i) 2239 warn("set enable/disable: setsockopt(IP_FW_SET_ENABLE)"); 2240 } else 2241 errx(EX_USAGE, "invalid set command %s\n", *av); 2242 } 2243 2244 void 2245 ipfw_sysctl_handler(char *av[], int which) 2246 { 2247 av++; 2248 2249 if (av[0] == NULL) { 2250 warnx("missing keyword to enable/disable\n"); 2251 } else if (_substrcmp(*av, "firewall") == 0) { 2252 sysctlbyname("net.inet.ip.fw.enable", NULL, 0, 2253 &which, sizeof(which)); 2254 sysctlbyname("net.inet6.ip6.fw.enable", NULL, 0, 2255 &which, sizeof(which)); 2256 } else if (_substrcmp(*av, "one_pass") == 0) { 2257 sysctlbyname("net.inet.ip.fw.one_pass", NULL, 0, 2258 &which, sizeof(which)); 2259 } else if (_substrcmp(*av, "debug") == 0) { 2260 sysctlbyname("net.inet.ip.fw.debug", NULL, 0, 2261 &which, sizeof(which)); 2262 } else if (_substrcmp(*av, "verbose") == 0) { 2263 sysctlbyname("net.inet.ip.fw.verbose", NULL, 0, 2264 &which, sizeof(which)); 2265 } else if (_substrcmp(*av, "dyn_keepalive") == 0) { 2266 sysctlbyname("net.inet.ip.fw.dyn_keepalive", NULL, 0, 2267 &which, sizeof(which)); 2268 #ifndef NO_ALTQ 2269 } else if (_substrcmp(*av, "altq") == 0) { 2270 altq_set_enabled(which); 2271 #endif 2272 } else { 2273 warnx("unrecognize enable/disable keyword: %s\n", *av); 2274 } 2275 } 2276 2277 typedef void state_cb(struct cmdline_opts *co, struct format_opts *fo, 2278 void *arg, void *state); 2279 2280 static void 2281 prepare_format_dyn(struct cmdline_opts *co, struct format_opts *fo, 2282 void *arg, void *_state) 2283 { 2284 ipfw_dyn_rule *d; 2285 int width; 2286 uint8_t set; 2287 2288 d = (ipfw_dyn_rule *)_state; 2289 /* Count _ALL_ states */ 2290 fo->dcnt++; 2291 2292 if (fo->show_counters == 0) 2293 return; 2294 2295 if (co->use_set) { 2296 /* skip states from another set */ 2297 bcopy((char *)&d->rule + sizeof(uint16_t), &set, 2298 sizeof(uint8_t)); 2299 if (set != co->use_set - 1) 2300 return; 2301 } 2302 2303 width = pr_u64(NULL, &d->pcnt, 0); 2304 if (width > fo->pcwidth) 2305 fo->pcwidth = width; 2306 2307 width = pr_u64(NULL, &d->bcnt, 0); 2308 if (width > fo->bcwidth) 2309 fo->bcwidth = width; 2310 } 2311 2312 static int 2313 foreach_state(struct cmdline_opts *co, struct format_opts *fo, 2314 caddr_t base, size_t sz, state_cb dyn_bc, void *dyn_arg) 2315 { 2316 int ttype; 2317 state_cb *fptr; 2318 void *farg; 2319 ipfw_obj_tlv *tlv; 2320 ipfw_obj_ctlv *ctlv; 2321 2322 fptr = NULL; 2323 ttype = 0; 2324 2325 while (sz > 0) { 2326 ctlv = (ipfw_obj_ctlv *)base; 2327 switch (ctlv->head.type) { 2328 case IPFW_TLV_DYNSTATE_LIST: 2329 base += sizeof(*ctlv); 2330 sz -= sizeof(*ctlv); 2331 ttype = IPFW_TLV_DYN_ENT; 2332 fptr = dyn_bc; 2333 farg = dyn_arg; 2334 break; 2335 default: 2336 return (sz); 2337 } 2338 2339 while (sz > 0) { 2340 tlv = (ipfw_obj_tlv *)base; 2341 if (tlv->type != ttype) 2342 break; 2343 2344 fptr(co, fo, farg, tlv + 1); 2345 sz -= tlv->length; 2346 base += tlv->length; 2347 } 2348 } 2349 2350 return (sz); 2351 } 2352 2353 static void 2354 prepare_format_opts(struct cmdline_opts *co, struct format_opts *fo, 2355 ipfw_obj_tlv *rtlv, int rcnt, caddr_t dynbase, size_t dynsz) 2356 { 2357 int bcwidth, pcwidth, width; 2358 int n; 2359 struct ip_fw_bcounter *cntr; 2360 struct ip_fw_rule *r; 2361 2362 bcwidth = 0; 2363 pcwidth = 0; 2364 if (fo->show_counters != 0) { 2365 for (n = 0; n < rcnt; n++, 2366 rtlv = (ipfw_obj_tlv *)((caddr_t)rtlv + rtlv->length)) { 2367 cntr = (struct ip_fw_bcounter *)(rtlv + 1); 2368 r = (struct ip_fw_rule *)((caddr_t)cntr + cntr->size); 2369 /* skip rules from another set */ 2370 if (co->use_set && r->set != co->use_set - 1) 2371 continue; 2372 2373 /* packet counter */ 2374 width = pr_u64(NULL, &cntr->pcnt, 0); 2375 if (width > pcwidth) 2376 pcwidth = width; 2377 2378 /* byte counter */ 2379 width = pr_u64(NULL, &cntr->bcnt, 0); 2380 if (width > bcwidth) 2381 bcwidth = width; 2382 } 2383 } 2384 fo->bcwidth = bcwidth; 2385 fo->pcwidth = pcwidth; 2386 2387 fo->dcnt = 0; 2388 if (co->do_dynamic && dynsz > 0) 2389 foreach_state(co, fo, dynbase, dynsz, prepare_format_dyn, NULL); 2390 } 2391 2392 static int 2393 list_static_range(struct cmdline_opts *co, struct format_opts *fo, 2394 struct buf_pr *bp, ipfw_obj_tlv *rtlv, int rcnt) 2395 { 2396 int n, seen; 2397 struct ip_fw_rule *r; 2398 struct ip_fw_bcounter *cntr; 2399 int c = 0; 2400 2401 for (n = seen = 0; n < rcnt; n++, 2402 rtlv = (ipfw_obj_tlv *)((caddr_t)rtlv + rtlv->length)) { 2403 2404 if (fo->show_counters != 0) { 2405 cntr = (struct ip_fw_bcounter *)(rtlv + 1); 2406 r = (struct ip_fw_rule *)((caddr_t)cntr + cntr->size); 2407 } else { 2408 cntr = NULL; 2409 r = (struct ip_fw_rule *)(rtlv + 1); 2410 } 2411 if (r->rulenum > fo->last) 2412 break; 2413 if (co->use_set && r->set != co->use_set - 1) 2414 continue; 2415 if (r->rulenum >= fo->first && r->rulenum <= fo->last) { 2416 show_static_rule(co, fo, bp, r, cntr); 2417 printf("%s", bp->buf); 2418 c += rtlv->length; 2419 bp_flush(bp); 2420 seen++; 2421 } 2422 } 2423 2424 return (seen); 2425 } 2426 2427 static void 2428 list_dyn_state(struct cmdline_opts *co, struct format_opts *fo, 2429 void *_arg, void *_state) 2430 { 2431 uint16_t rulenum; 2432 uint8_t set; 2433 ipfw_dyn_rule *d; 2434 struct buf_pr *bp; 2435 2436 d = (ipfw_dyn_rule *)_state; 2437 bp = (struct buf_pr *)_arg; 2438 2439 bcopy(&d->rule, &rulenum, sizeof(rulenum)); 2440 if (rulenum > fo->last) 2441 return; 2442 if (co->use_set) { 2443 bcopy((char *)&d->rule + sizeof(uint16_t), 2444 &set, sizeof(uint8_t)); 2445 if (set != co->use_set - 1) 2446 return; 2447 } 2448 if (rulenum >= fo->first) { 2449 show_dyn_state(co, fo, bp, d); 2450 printf("%s\n", bp->buf); 2451 bp_flush(bp); 2452 } 2453 } 2454 2455 static int 2456 list_dyn_range(struct cmdline_opts *co, struct format_opts *fo, 2457 struct buf_pr *bp, caddr_t base, size_t sz) 2458 { 2459 2460 sz = foreach_state(co, fo, base, sz, list_dyn_state, bp); 2461 return (sz); 2462 } 2463 2464 void 2465 ipfw_list(int ac, char *av[], int show_counters) 2466 { 2467 ipfw_cfg_lheader *cfg; 2468 struct format_opts sfo; 2469 size_t sz; 2470 int error; 2471 int lac; 2472 char **lav; 2473 uint32_t rnum; 2474 char *endptr; 2475 2476 if (co.test_only) { 2477 fprintf(stderr, "Testing only, list disabled\n"); 2478 return; 2479 } 2480 if (co.do_pipe) { 2481 dummynet_list(ac, av, show_counters); 2482 return; 2483 } 2484 2485 ac--; 2486 av++; 2487 memset(&sfo, 0, sizeof(sfo)); 2488 2489 /* Determine rule range to request */ 2490 if (ac > 0) { 2491 for (lac = ac, lav = av; lac != 0; lac--) { 2492 rnum = strtoul(*lav++, &endptr, 10); 2493 if (sfo.first == 0 || rnum < sfo.first) 2494 sfo.first = rnum; 2495 2496 if (*endptr == '-') 2497 rnum = strtoul(endptr + 1, &endptr, 10); 2498 if (sfo.last == 0 || rnum > sfo.last) 2499 sfo.last = rnum; 2500 } 2501 } 2502 2503 /* get configuraion from kernel */ 2504 cfg = NULL; 2505 sfo.show_counters = show_counters; 2506 sfo.flags = IPFW_CFG_GET_STATIC; 2507 if (co.do_dynamic != 0) 2508 sfo.flags |= IPFW_CFG_GET_STATES; 2509 if (sfo.show_counters != 0) 2510 sfo.flags |= IPFW_CFG_GET_COUNTERS; 2511 if (ipfw_get_config(&co, &sfo, &cfg, &sz) != 0) 2512 err(EX_OSERR, "retrieving config failed"); 2513 2514 error = ipfw_show_config(&co, &sfo, cfg, sz, ac, av); 2515 2516 free(cfg); 2517 2518 if (error != EX_OK) 2519 exit(error); 2520 } 2521 2522 static int 2523 ipfw_show_config(struct cmdline_opts *co, struct format_opts *fo, 2524 ipfw_cfg_lheader *cfg, size_t sz, int ac, char *av[]) 2525 { 2526 caddr_t dynbase; 2527 size_t dynsz; 2528 int rcnt; 2529 int exitval = EX_OK; 2530 int lac; 2531 char **lav; 2532 char *endptr; 2533 size_t read; 2534 struct buf_pr bp; 2535 ipfw_obj_ctlv *ctlv, *tstate; 2536 ipfw_obj_tlv *rbase; 2537 2538 /* 2539 * Handle tablenames TLV first, if any 2540 */ 2541 tstate = NULL; 2542 rbase = NULL; 2543 dynbase = NULL; 2544 dynsz = 0; 2545 read = sizeof(*cfg); 2546 2547 fo->set_mask = cfg->set_mask; 2548 2549 ctlv = (ipfw_obj_ctlv *)(cfg + 1); 2550 2551 if (cfg->flags & IPFW_CFG_GET_STATIC) { 2552 /* We've requested static rules */ 2553 if (ctlv->head.type == IPFW_TLV_TBLNAME_LIST) { 2554 fo->tstate = ctlv; 2555 read += ctlv->head.length; 2556 ctlv = (ipfw_obj_ctlv *)((caddr_t)ctlv + 2557 ctlv->head.length); 2558 } 2559 2560 if (ctlv->head.type == IPFW_TLV_RULE_LIST) { 2561 rbase = (ipfw_obj_tlv *)(ctlv + 1); 2562 rcnt = ctlv->count; 2563 read += ctlv->head.length; 2564 ctlv = (ipfw_obj_ctlv *)((caddr_t)ctlv + 2565 ctlv->head.length); 2566 } 2567 } 2568 2569 if ((cfg->flags & IPFW_CFG_GET_STATES) && (read != sz)) { 2570 /* We may have some dynamic states */ 2571 dynsz = sz - read; 2572 /* Skip empty header */ 2573 if (dynsz != sizeof(ipfw_obj_ctlv)) 2574 dynbase = (caddr_t)ctlv; 2575 else 2576 dynsz = 0; 2577 } 2578 2579 prepare_format_opts(co, fo, rbase, rcnt, dynbase, dynsz); 2580 bp_alloc(&bp, 4096); 2581 2582 /* if no rule numbers were specified, list all rules */ 2583 if (ac == 0) { 2584 fo->first = 0; 2585 fo->last = IPFW_DEFAULT_RULE; 2586 list_static_range(co, fo, &bp, rbase, rcnt); 2587 2588 if (co->do_dynamic && dynsz > 0) { 2589 printf("## Dynamic rules (%d %zu):\n", fo->dcnt, dynsz); 2590 list_dyn_range(co, fo, &bp, dynbase, dynsz); 2591 } 2592 2593 bp_free(&bp); 2594 return (EX_OK); 2595 } 2596 2597 /* display specific rules requested on command line */ 2598 for (lac = ac, lav = av; lac != 0; lac--) { 2599 /* convert command line rule # */ 2600 fo->last = fo->first = strtoul(*lav++, &endptr, 10); 2601 if (*endptr == '-') 2602 fo->last = strtoul(endptr + 1, &endptr, 10); 2603 if (*endptr) { 2604 exitval = EX_USAGE; 2605 warnx("invalid rule number: %s", *(lav - 1)); 2606 continue; 2607 } 2608 2609 if (list_static_range(co, fo, &bp, rbase, rcnt) == 0) { 2610 /* give precedence to other error(s) */ 2611 if (exitval == EX_OK) 2612 exitval = EX_UNAVAILABLE; 2613 if (fo->first == fo->last) 2614 warnx("rule %u does not exist", fo->first); 2615 else 2616 warnx("no rules in range %u-%u", 2617 fo->first, fo->last); 2618 } 2619 } 2620 2621 if (co->do_dynamic && dynsz > 0) { 2622 printf("## Dynamic rules:\n"); 2623 for (lac = ac, lav = av; lac != 0; lac--) { 2624 fo->last = fo->first = strtoul(*lav++, &endptr, 10); 2625 if (*endptr == '-') 2626 fo->last = strtoul(endptr+1, &endptr, 10); 2627 if (*endptr) 2628 /* already warned */ 2629 continue; 2630 list_dyn_range(co, fo, &bp, dynbase, dynsz); 2631 } 2632 } 2633 2634 bp_free(&bp); 2635 return (exitval); 2636 } 2637 2638 2639 /* 2640 * Retrieves current ipfw configuration of given type 2641 * and stores its pointer to @pcfg. 2642 * 2643 * Caller is responsible for freeing @pcfg. 2644 * 2645 * Returns 0 on success. 2646 */ 2647 2648 static int 2649 ipfw_get_config(struct cmdline_opts *co, struct format_opts *fo, 2650 ipfw_cfg_lheader **pcfg, size_t *psize) 2651 { 2652 ipfw_cfg_lheader *cfg; 2653 size_t sz; 2654 int i; 2655 2656 2657 if (co->test_only != 0) { 2658 fprintf(stderr, "Testing only, list disabled\n"); 2659 return (0); 2660 } 2661 2662 /* Start with some data size */ 2663 sz = 4096; 2664 cfg = NULL; 2665 2666 for (i = 0; i < 16; i++) { 2667 if (cfg != NULL) 2668 free(cfg); 2669 if ((cfg = calloc(1, sz)) == NULL) 2670 return (ENOMEM); 2671 2672 cfg->flags = fo->flags; 2673 cfg->start_rule = fo->first; 2674 cfg->end_rule = fo->last; 2675 2676 if (do_get3(IP_FW_XGET, &cfg->opheader, &sz) != 0) { 2677 if (errno != ENOMEM) { 2678 free(cfg); 2679 return (errno); 2680 } 2681 2682 /* Buffer size is not enough. Try to increase */ 2683 sz = sz * 2; 2684 if (sz < cfg->size) 2685 sz = cfg->size; 2686 continue; 2687 } 2688 2689 *pcfg = cfg; 2690 *psize = sz; 2691 return (0); 2692 } 2693 2694 free(cfg); 2695 return (ENOMEM); 2696 } 2697 2698 static int 2699 lookup_host (char *host, struct in_addr *ipaddr) 2700 { 2701 struct hostent *he; 2702 2703 if (!inet_aton(host, ipaddr)) { 2704 if ((he = gethostbyname(host)) == NULL) 2705 return(-1); 2706 *ipaddr = *(struct in_addr *)he->h_addr_list[0]; 2707 } 2708 return(0); 2709 } 2710 2711 struct tidx { 2712 ipfw_obj_ntlv *idx; 2713 uint32_t count; 2714 uint32_t size; 2715 uint16_t counter; 2716 uint8_t set; 2717 }; 2718 2719 static uint16_t 2720 pack_table(struct tidx *tstate, char *name) 2721 { 2722 int i; 2723 ipfw_obj_ntlv *ntlv; 2724 2725 if (table_check_name(name) != 0) 2726 return (0); 2727 2728 for (i = 0; i < tstate->count; i++) { 2729 if (strcmp(tstate->idx[i].name, name) != 0) 2730 continue; 2731 if (tstate->idx[i].set != tstate->set) 2732 continue; 2733 2734 return (tstate->idx[i].idx); 2735 } 2736 2737 if (tstate->count + 1 > tstate->size) { 2738 tstate->size += 4; 2739 tstate->idx = realloc(tstate->idx, tstate->size * 2740 sizeof(ipfw_obj_ntlv)); 2741 if (tstate->idx == NULL) 2742 return (0); 2743 } 2744 2745 ntlv = &tstate->idx[i]; 2746 memset(ntlv, 0, sizeof(ipfw_obj_ntlv)); 2747 strlcpy(ntlv->name, name, sizeof(ntlv->name)); 2748 ntlv->head.type = IPFW_TLV_TBL_NAME; 2749 ntlv->head.length = sizeof(ipfw_obj_ntlv); 2750 ntlv->set = tstate->set; 2751 ntlv->idx = ++tstate->counter; 2752 tstate->count++; 2753 2754 return (ntlv->idx); 2755 } 2756 2757 static void 2758 fill_table(ipfw_insn *cmd, char *av, uint8_t opcode, struct tidx *tstate) 2759 { 2760 uint32_t *d = ((ipfw_insn_u32 *)cmd)->d; 2761 uint16_t uidx; 2762 char *p; 2763 2764 if ((p = strchr(av + 6, ')')) == NULL) 2765 errx(EX_DATAERR, "forgotten parenthesis: '%s'", av); 2766 *p = '\0'; 2767 p = strchr(av + 6, ','); 2768 if (p) 2769 *p++ = '\0'; 2770 2771 if ((uidx = pack_table(tstate, av + 6)) == 0) 2772 errx(EX_DATAERR, "Invalid table name: %s", av + 6); 2773 2774 cmd->opcode = opcode; 2775 cmd->arg1 = uidx; 2776 if (p) { 2777 cmd->len |= F_INSN_SIZE(ipfw_insn_u32); 2778 d[0] = strtoul(p, NULL, 0); 2779 } else 2780 cmd->len |= F_INSN_SIZE(ipfw_insn); 2781 } 2782 2783 2784 /* 2785 * fills the addr and mask fields in the instruction as appropriate from av. 2786 * Update length as appropriate. 2787 * The following formats are allowed: 2788 * me returns O_IP_*_ME 2789 * 1.2.3.4 single IP address 2790 * 1.2.3.4:5.6.7.8 address:mask 2791 * 1.2.3.4/24 address/mask 2792 * 1.2.3.4/26{1,6,5,4,23} set of addresses in a subnet 2793 * We can have multiple comma-separated address/mask entries. 2794 */ 2795 static void 2796 fill_ip(ipfw_insn_ip *cmd, char *av, int cblen, struct tidx *tstate) 2797 { 2798 int len = 0; 2799 uint32_t *d = ((ipfw_insn_u32 *)cmd)->d; 2800 2801 cmd->o.len &= ~F_LEN_MASK; /* zero len */ 2802 2803 if (_substrcmp(av, "any") == 0) 2804 return; 2805 2806 if (_substrcmp(av, "me") == 0) { 2807 cmd->o.len |= F_INSN_SIZE(ipfw_insn); 2808 return; 2809 } 2810 2811 if (strncmp(av, "table(", 6) == 0) { 2812 fill_table(&cmd->o, av, O_IP_DST_LOOKUP, tstate); 2813 return; 2814 } 2815 2816 while (av) { 2817 /* 2818 * After the address we can have '/' or ':' indicating a mask, 2819 * ',' indicating another address follows, '{' indicating a 2820 * set of addresses of unspecified size. 2821 */ 2822 char *t = NULL, *p = strpbrk(av, "/:,{"); 2823 int masklen; 2824 char md, nd = '\0'; 2825 2826 CHECK_LENGTH(cblen, F_INSN_SIZE(ipfw_insn) + 2 + len); 2827 2828 if (p) { 2829 md = *p; 2830 *p++ = '\0'; 2831 if ((t = strpbrk(p, ",{")) != NULL) { 2832 nd = *t; 2833 *t = '\0'; 2834 } 2835 } else 2836 md = '\0'; 2837 2838 if (lookup_host(av, (struct in_addr *)&d[0]) != 0) 2839 errx(EX_NOHOST, "hostname ``%s'' unknown", av); 2840 switch (md) { 2841 case ':': 2842 if (!inet_aton(p, (struct in_addr *)&d[1])) 2843 errx(EX_DATAERR, "bad netmask ``%s''", p); 2844 break; 2845 case '/': 2846 masklen = atoi(p); 2847 if (masklen == 0) 2848 d[1] = htonl(0); /* mask */ 2849 else if (masklen > 32) 2850 errx(EX_DATAERR, "bad width ``%s''", p); 2851 else 2852 d[1] = htonl(~0 << (32 - masklen)); 2853 break; 2854 case '{': /* no mask, assume /24 and put back the '{' */ 2855 d[1] = htonl(~0 << (32 - 24)); 2856 *(--p) = md; 2857 break; 2858 2859 case ',': /* single address plus continuation */ 2860 *(--p) = md; 2861 /* FALLTHROUGH */ 2862 case 0: /* initialization value */ 2863 default: 2864 d[1] = htonl(~0); /* force /32 */ 2865 break; 2866 } 2867 d[0] &= d[1]; /* mask base address with mask */ 2868 if (t) 2869 *t = nd; 2870 /* find next separator */ 2871 if (p) 2872 p = strpbrk(p, ",{"); 2873 if (p && *p == '{') { 2874 /* 2875 * We have a set of addresses. They are stored as follows: 2876 * arg1 is the set size (powers of 2, 2..256) 2877 * addr is the base address IN HOST FORMAT 2878 * mask.. is an array of arg1 bits (rounded up to 2879 * the next multiple of 32) with bits set 2880 * for each host in the map. 2881 */ 2882 uint32_t *map = (uint32_t *)&cmd->mask; 2883 int low, high; 2884 int i = contigmask((uint8_t *)&(d[1]), 32); 2885 2886 if (len > 0) 2887 errx(EX_DATAERR, "address set cannot be in a list"); 2888 if (i < 24 || i > 31) 2889 errx(EX_DATAERR, "invalid set with mask %d\n", i); 2890 cmd->o.arg1 = 1<<(32-i); /* map length */ 2891 d[0] = ntohl(d[0]); /* base addr in host format */ 2892 cmd->o.opcode = O_IP_DST_SET; /* default */ 2893 cmd->o.len |= F_INSN_SIZE(ipfw_insn_u32) + (cmd->o.arg1+31)/32; 2894 for (i = 0; i < (cmd->o.arg1+31)/32 ; i++) 2895 map[i] = 0; /* clear map */ 2896 2897 av = p + 1; 2898 low = d[0] & 0xff; 2899 high = low + cmd->o.arg1 - 1; 2900 /* 2901 * Here, i stores the previous value when we specify a range 2902 * of addresses within a mask, e.g. 45-63. i = -1 means we 2903 * have no previous value. 2904 */ 2905 i = -1; /* previous value in a range */ 2906 while (isdigit(*av)) { 2907 char *s; 2908 int a = strtol(av, &s, 0); 2909 2910 if (s == av) { /* no parameter */ 2911 if (*av != '}') 2912 errx(EX_DATAERR, "set not closed\n"); 2913 if (i != -1) 2914 errx(EX_DATAERR, "incomplete range %d-", i); 2915 break; 2916 } 2917 if (a < low || a > high) 2918 errx(EX_DATAERR, "addr %d out of range [%d-%d]\n", 2919 a, low, high); 2920 a -= low; 2921 if (i == -1) /* no previous in range */ 2922 i = a; 2923 else { /* check that range is valid */ 2924 if (i > a) 2925 errx(EX_DATAERR, "invalid range %d-%d", 2926 i+low, a+low); 2927 if (*s == '-') 2928 errx(EX_DATAERR, "double '-' in range"); 2929 } 2930 for (; i <= a; i++) 2931 map[i/32] |= 1<<(i & 31); 2932 i = -1; 2933 if (*s == '-') 2934 i = a; 2935 else if (*s == '}') 2936 break; 2937 av = s+1; 2938 } 2939 return; 2940 } 2941 av = p; 2942 if (av) /* then *av must be a ',' */ 2943 av++; 2944 2945 /* Check this entry */ 2946 if (d[1] == 0) { /* "any", specified as x.x.x.x/0 */ 2947 /* 2948 * 'any' turns the entire list into a NOP. 2949 * 'not any' never matches, so it is removed from the 2950 * list unless it is the only item, in which case we 2951 * report an error. 2952 */ 2953 if (cmd->o.len & F_NOT) { /* "not any" never matches */ 2954 if (av == NULL && len == 0) /* only this entry */ 2955 errx(EX_DATAERR, "not any never matches"); 2956 } 2957 /* else do nothing and skip this entry */ 2958 return; 2959 } 2960 /* A single IP can be stored in an optimized format */ 2961 if (d[1] == (uint32_t)~0 && av == NULL && len == 0) { 2962 cmd->o.len |= F_INSN_SIZE(ipfw_insn_u32); 2963 return; 2964 } 2965 len += 2; /* two words... */ 2966 d += 2; 2967 } /* end while */ 2968 if (len + 1 > F_LEN_MASK) 2969 errx(EX_DATAERR, "address list too long"); 2970 cmd->o.len |= len+1; 2971 } 2972 2973 2974 /* n2mask sets n bits of the mask */ 2975 void 2976 n2mask(struct in6_addr *mask, int n) 2977 { 2978 static int minimask[9] = 2979 { 0x00, 0x80, 0xc0, 0xe0, 0xf0, 0xf8, 0xfc, 0xfe, 0xff }; 2980 u_char *p; 2981 2982 memset(mask, 0, sizeof(struct in6_addr)); 2983 p = (u_char *) mask; 2984 for (; n > 0; p++, n -= 8) { 2985 if (n >= 8) 2986 *p = 0xff; 2987 else 2988 *p = minimask[n]; 2989 } 2990 return; 2991 } 2992 2993 static void 2994 fill_flags_cmd(ipfw_insn *cmd, enum ipfw_opcodes opcode, 2995 struct _s_x *flags, char *p) 2996 { 2997 char *e; 2998 uint32_t set = 0, clear = 0; 2999 3000 if (fill_flags(flags, p, &e, &set, &clear) != 0) 3001 errx(EX_DATAERR, "invalid flag %s", e); 3002 3003 cmd->opcode = opcode; 3004 cmd->len = (cmd->len & (F_NOT | F_OR)) | 1; 3005 cmd->arg1 = (set & 0xff) | ( (clear & 0xff) << 8); 3006 } 3007 3008 3009 void 3010 ipfw_delete(char *av[]) 3011 { 3012 int i; 3013 int exitval = EX_OK; 3014 int do_set = 0; 3015 ipfw_range_tlv rt; 3016 3017 av++; 3018 NEED1("missing rule specification"); 3019 memset(&rt, 0, sizeof(rt)); 3020 if ( *av && _substrcmp(*av, "set") == 0) { 3021 /* Do not allow using the following syntax: 3022 * ipfw set N delete set M 3023 */ 3024 if (co.use_set) 3025 errx(EX_DATAERR, "invalid syntax"); 3026 do_set = 1; /* delete set */ 3027 av++; 3028 } 3029 3030 /* Rule number */ 3031 while (*av && isdigit(**av)) { 3032 i = atoi(*av); av++; 3033 if (co.do_nat) { 3034 exitval = do_cmd(IP_FW_NAT_DEL, &i, sizeof i); 3035 if (exitval) { 3036 exitval = EX_UNAVAILABLE; 3037 warn("rule %u not available", i); 3038 } 3039 } else if (co.do_pipe) { 3040 exitval = ipfw_delete_pipe(co.do_pipe, i); 3041 } else { 3042 if (do_set != 0) { 3043 rt.set = i & 31; 3044 rt.flags = IPFW_RCFLAG_SET; 3045 } else { 3046 rt.start_rule = i & 0xffff; 3047 rt.end_rule = i & 0xffff; 3048 if (rt.start_rule == 0 && rt.end_rule == 0) 3049 rt.flags |= IPFW_RCFLAG_ALL; 3050 else 3051 rt.flags |= IPFW_RCFLAG_RANGE; 3052 if (co.use_set != 0) { 3053 rt.set = co.use_set - 1; 3054 rt.flags |= IPFW_RCFLAG_SET; 3055 } 3056 } 3057 i = do_range_cmd(IP_FW_XDEL, &rt); 3058 if (i != 0) { 3059 exitval = EX_UNAVAILABLE; 3060 warn("rule %u: setsockopt(IP_FW_XDEL)", 3061 rt.start_rule); 3062 } else if (rt.new_set == 0) { 3063 exitval = EX_UNAVAILABLE; 3064 if (rt.start_rule != rt.end_rule) 3065 warnx("no rules rules in %u-%u range", 3066 rt.start_rule, rt.end_rule); 3067 else 3068 warnx("rule %u not found", 3069 rt.start_rule); 3070 } 3071 } 3072 } 3073 if (exitval != EX_OK) 3074 exit(exitval); 3075 } 3076 3077 3078 /* 3079 * fill the interface structure. We do not check the name as we can 3080 * create interfaces dynamically, so checking them at insert time 3081 * makes relatively little sense. 3082 * Interface names containing '*', '?', or '[' are assumed to be shell 3083 * patterns which match interfaces. 3084 */ 3085 static void 3086 fill_iface(ipfw_insn_if *cmd, char *arg, int cblen, struct tidx *tstate) 3087 { 3088 char *p; 3089 uint16_t uidx; 3090 3091 cmd->name[0] = '\0'; 3092 cmd->o.len |= F_INSN_SIZE(ipfw_insn_if); 3093 3094 CHECK_CMDLEN; 3095 3096 /* Parse the interface or address */ 3097 if (strcmp(arg, "any") == 0) 3098 cmd->o.len = 0; /* effectively ignore this command */ 3099 else if (strncmp(arg, "table(", 6) == 0) { 3100 if ((p = strchr(arg + 6, ')')) == NULL) 3101 errx(EX_DATAERR, "forgotten parenthesis: '%s'", arg); 3102 *p = '\0'; 3103 p = strchr(arg + 6, ','); 3104 if (p) 3105 *p++ = '\0'; 3106 if ((uidx = pack_table(tstate, arg + 6)) == 0) 3107 errx(EX_DATAERR, "Invalid table name: %s", arg + 6); 3108 3109 cmd->name[0] = '\1'; /* Special value indicating table */ 3110 cmd->p.kidx = uidx; 3111 } else if (!isdigit(*arg)) { 3112 strlcpy(cmd->name, arg, sizeof(cmd->name)); 3113 cmd->p.glob = strpbrk(arg, "*?[") != NULL ? 1 : 0; 3114 } else if (!inet_aton(arg, &cmd->p.ip)) 3115 errx(EX_DATAERR, "bad ip address ``%s''", arg); 3116 } 3117 3118 static void 3119 get_mac_addr_mask(const char *p, uint8_t *addr, uint8_t *mask) 3120 { 3121 int i; 3122 size_t l; 3123 char *ap, *ptr, *optr; 3124 struct ether_addr *mac; 3125 const char *macset = "0123456789abcdefABCDEF:"; 3126 3127 if (strcmp(p, "any") == 0) { 3128 for (i = 0; i < ETHER_ADDR_LEN; i++) 3129 addr[i] = mask[i] = 0; 3130 return; 3131 } 3132 3133 optr = ptr = strdup(p); 3134 if ((ap = strsep(&ptr, "&/")) != NULL && *ap != 0) { 3135 l = strlen(ap); 3136 if (strspn(ap, macset) != l || (mac = ether_aton(ap)) == NULL) 3137 errx(EX_DATAERR, "Incorrect MAC address"); 3138 bcopy(mac, addr, ETHER_ADDR_LEN); 3139 } else 3140 errx(EX_DATAERR, "Incorrect MAC address"); 3141 3142 if (ptr != NULL) { /* we have mask? */ 3143 if (p[ptr - optr - 1] == '/') { /* mask len */ 3144 long ml = strtol(ptr, &ap, 10); 3145 if (*ap != 0 || ml > ETHER_ADDR_LEN * 8 || ml < 0) 3146 errx(EX_DATAERR, "Incorrect mask length"); 3147 for (i = 0; ml > 0 && i < ETHER_ADDR_LEN; ml -= 8, i++) 3148 mask[i] = (ml >= 8) ? 0xff: (~0) << (8 - ml); 3149 } else { /* mask */ 3150 l = strlen(ptr); 3151 if (strspn(ptr, macset) != l || 3152 (mac = ether_aton(ptr)) == NULL) 3153 errx(EX_DATAERR, "Incorrect mask"); 3154 bcopy(mac, mask, ETHER_ADDR_LEN); 3155 } 3156 } else { /* default mask: ff:ff:ff:ff:ff:ff */ 3157 for (i = 0; i < ETHER_ADDR_LEN; i++) 3158 mask[i] = 0xff; 3159 } 3160 for (i = 0; i < ETHER_ADDR_LEN; i++) 3161 addr[i] &= mask[i]; 3162 3163 free(optr); 3164 } 3165 3166 /* 3167 * helper function, updates the pointer to cmd with the length 3168 * of the current command, and also cleans up the first word of 3169 * the new command in case it has been clobbered before. 3170 */ 3171 static ipfw_insn * 3172 next_cmd(ipfw_insn *cmd, int *len) 3173 { 3174 *len -= F_LEN(cmd); 3175 CHECK_LENGTH(*len, 0); 3176 cmd += F_LEN(cmd); 3177 bzero(cmd, sizeof(*cmd)); 3178 return cmd; 3179 } 3180 3181 /* 3182 * Takes arguments and copies them into a comment 3183 */ 3184 static void 3185 fill_comment(ipfw_insn *cmd, char **av, int cblen) 3186 { 3187 int i, l; 3188 char *p = (char *)(cmd + 1); 3189 3190 cmd->opcode = O_NOP; 3191 cmd->len = (cmd->len & (F_NOT | F_OR)); 3192 3193 /* Compute length of comment string. */ 3194 for (i = 0, l = 0; av[i] != NULL; i++) 3195 l += strlen(av[i]) + 1; 3196 if (l == 0) 3197 return; 3198 if (l > 84) 3199 errx(EX_DATAERR, 3200 "comment too long (max 80 chars)"); 3201 l = 1 + (l+3)/4; 3202 cmd->len = (cmd->len & (F_NOT | F_OR)) | l; 3203 CHECK_CMDLEN; 3204 3205 for (i = 0; av[i] != NULL; i++) { 3206 strcpy(p, av[i]); 3207 p += strlen(av[i]); 3208 *p++ = ' '; 3209 } 3210 *(--p) = '\0'; 3211 } 3212 3213 /* 3214 * A function to fill simple commands of size 1. 3215 * Existing flags are preserved. 3216 */ 3217 static void 3218 fill_cmd(ipfw_insn *cmd, enum ipfw_opcodes opcode, int flags, uint16_t arg) 3219 { 3220 cmd->opcode = opcode; 3221 cmd->len = ((cmd->len | flags) & (F_NOT | F_OR)) | 1; 3222 cmd->arg1 = arg; 3223 } 3224 3225 /* 3226 * Fetch and add the MAC address and type, with masks. This generates one or 3227 * two microinstructions, and returns the pointer to the last one. 3228 */ 3229 static ipfw_insn * 3230 add_mac(ipfw_insn *cmd, char *av[], int cblen) 3231 { 3232 ipfw_insn_mac *mac; 3233 3234 if ( ( av[0] == NULL ) || ( av[1] == NULL ) ) 3235 errx(EX_DATAERR, "MAC dst src"); 3236 3237 cmd->opcode = O_MACADDR2; 3238 cmd->len = (cmd->len & (F_NOT | F_OR)) | F_INSN_SIZE(ipfw_insn_mac); 3239 CHECK_CMDLEN; 3240 3241 mac = (ipfw_insn_mac *)cmd; 3242 get_mac_addr_mask(av[0], mac->addr, mac->mask); /* dst */ 3243 get_mac_addr_mask(av[1], &(mac->addr[ETHER_ADDR_LEN]), 3244 &(mac->mask[ETHER_ADDR_LEN])); /* src */ 3245 return cmd; 3246 } 3247 3248 static ipfw_insn * 3249 add_mactype(ipfw_insn *cmd, char *av, int cblen) 3250 { 3251 if (!av) 3252 errx(EX_DATAERR, "missing MAC type"); 3253 if (strcmp(av, "any") != 0) { /* we have a non-null type */ 3254 fill_newports((ipfw_insn_u16 *)cmd, av, IPPROTO_ETHERTYPE, 3255 cblen); 3256 cmd->opcode = O_MAC_TYPE; 3257 return cmd; 3258 } else 3259 return NULL; 3260 } 3261 3262 static ipfw_insn * 3263 add_proto0(ipfw_insn *cmd, char *av, u_char *protop) 3264 { 3265 struct protoent *pe; 3266 char *ep; 3267 int proto; 3268 3269 proto = strtol(av, &ep, 10); 3270 if (*ep != '\0' || proto <= 0) { 3271 if ((pe = getprotobyname(av)) == NULL) 3272 return NULL; 3273 proto = pe->p_proto; 3274 } 3275 3276 fill_cmd(cmd, O_PROTO, 0, proto); 3277 *protop = proto; 3278 return cmd; 3279 } 3280 3281 static ipfw_insn * 3282 add_proto(ipfw_insn *cmd, char *av, u_char *protop) 3283 { 3284 u_char proto = IPPROTO_IP; 3285 3286 if (_substrcmp(av, "all") == 0 || strcmp(av, "ip") == 0) 3287 ; /* do not set O_IP4 nor O_IP6 */ 3288 else if (strcmp(av, "ip4") == 0) 3289 /* explicit "just IPv4" rule */ 3290 fill_cmd(cmd, O_IP4, 0, 0); 3291 else if (strcmp(av, "ip6") == 0) { 3292 /* explicit "just IPv6" rule */ 3293 proto = IPPROTO_IPV6; 3294 fill_cmd(cmd, O_IP6, 0, 0); 3295 } else 3296 return add_proto0(cmd, av, protop); 3297 3298 *protop = proto; 3299 return cmd; 3300 } 3301 3302 static ipfw_insn * 3303 add_proto_compat(ipfw_insn *cmd, char *av, u_char *protop) 3304 { 3305 u_char proto = IPPROTO_IP; 3306 3307 if (_substrcmp(av, "all") == 0 || strcmp(av, "ip") == 0) 3308 ; /* do not set O_IP4 nor O_IP6 */ 3309 else if (strcmp(av, "ipv4") == 0 || strcmp(av, "ip4") == 0) 3310 /* explicit "just IPv4" rule */ 3311 fill_cmd(cmd, O_IP4, 0, 0); 3312 else if (strcmp(av, "ipv6") == 0 || strcmp(av, "ip6") == 0) { 3313 /* explicit "just IPv6" rule */ 3314 proto = IPPROTO_IPV6; 3315 fill_cmd(cmd, O_IP6, 0, 0); 3316 } else 3317 return add_proto0(cmd, av, protop); 3318 3319 *protop = proto; 3320 return cmd; 3321 } 3322 3323 static ipfw_insn * 3324 add_srcip(ipfw_insn *cmd, char *av, int cblen, struct tidx *tstate) 3325 { 3326 fill_ip((ipfw_insn_ip *)cmd, av, cblen, tstate); 3327 if (cmd->opcode == O_IP_DST_SET) /* set */ 3328 cmd->opcode = O_IP_SRC_SET; 3329 else if (cmd->opcode == O_IP_DST_LOOKUP) /* table */ 3330 cmd->opcode = O_IP_SRC_LOOKUP; 3331 else if (F_LEN(cmd) == F_INSN_SIZE(ipfw_insn)) /* me */ 3332 cmd->opcode = O_IP_SRC_ME; 3333 else if (F_LEN(cmd) == F_INSN_SIZE(ipfw_insn_u32)) /* one IP */ 3334 cmd->opcode = O_IP_SRC; 3335 else /* addr/mask */ 3336 cmd->opcode = O_IP_SRC_MASK; 3337 return cmd; 3338 } 3339 3340 static ipfw_insn * 3341 add_dstip(ipfw_insn *cmd, char *av, int cblen, struct tidx *tstate) 3342 { 3343 fill_ip((ipfw_insn_ip *)cmd, av, cblen, tstate); 3344 if (cmd->opcode == O_IP_DST_SET) /* set */ 3345 ; 3346 else if (cmd->opcode == O_IP_DST_LOOKUP) /* table */ 3347 ; 3348 else if (F_LEN(cmd) == F_INSN_SIZE(ipfw_insn)) /* me */ 3349 cmd->opcode = O_IP_DST_ME; 3350 else if (F_LEN(cmd) == F_INSN_SIZE(ipfw_insn_u32)) /* one IP */ 3351 cmd->opcode = O_IP_DST; 3352 else /* addr/mask */ 3353 cmd->opcode = O_IP_DST_MASK; 3354 return cmd; 3355 } 3356 3357 static struct _s_x f_reserved_keywords[] = { 3358 { "altq", TOK_OR }, 3359 { "//", TOK_OR }, 3360 { "diverted", TOK_OR }, 3361 { "dst-port", TOK_OR }, 3362 { "src-port", TOK_OR }, 3363 { "established", TOK_OR }, 3364 { "keep-state", TOK_OR }, 3365 { "frag", TOK_OR }, 3366 { "icmptypes", TOK_OR }, 3367 { "in", TOK_OR }, 3368 { "out", TOK_OR }, 3369 { "ip6", TOK_OR }, 3370 { "any", TOK_OR }, 3371 { "to", TOK_OR }, 3372 { "via", TOK_OR }, 3373 { "{", TOK_OR }, 3374 { NULL, 0 } /* terminator */ 3375 }; 3376 3377 static ipfw_insn * 3378 add_ports(ipfw_insn *cmd, char *av, u_char proto, int opcode, int cblen) 3379 { 3380 3381 if (match_token(f_reserved_keywords, av) != -1) 3382 return (NULL); 3383 3384 if (fill_newports((ipfw_insn_u16 *)cmd, av, proto, cblen)) { 3385 /* XXX todo: check that we have a protocol with ports */ 3386 cmd->opcode = opcode; 3387 return cmd; 3388 } 3389 return NULL; 3390 } 3391 3392 static ipfw_insn * 3393 add_src(ipfw_insn *cmd, char *av, u_char proto, int cblen, struct tidx *tstate) 3394 { 3395 struct in6_addr a; 3396 char *host, *ch, buf[INET6_ADDRSTRLEN]; 3397 ipfw_insn *ret = NULL; 3398 int len; 3399 3400 /* Copy first address in set if needed */ 3401 if ((ch = strpbrk(av, "/,")) != NULL) { 3402 len = ch - av; 3403 strlcpy(buf, av, sizeof(buf)); 3404 if (len < sizeof(buf)) 3405 buf[len] = '\0'; 3406 host = buf; 3407 } else 3408 host = av; 3409 3410 if (proto == IPPROTO_IPV6 || strcmp(av, "me6") == 0 || 3411 inet_pton(AF_INET6, host, &a) == 1) 3412 ret = add_srcip6(cmd, av, cblen); 3413 /* XXX: should check for IPv4, not !IPv6 */ 3414 if (ret == NULL && (proto == IPPROTO_IP || strcmp(av, "me") == 0 || 3415 inet_pton(AF_INET6, host, &a) != 1)) 3416 ret = add_srcip(cmd, av, cblen, tstate); 3417 if (ret == NULL && strcmp(av, "any") != 0) 3418 ret = cmd; 3419 3420 return ret; 3421 } 3422 3423 static ipfw_insn * 3424 add_dst(ipfw_insn *cmd, char *av, u_char proto, int cblen, struct tidx *tstate) 3425 { 3426 struct in6_addr a; 3427 char *host, *ch, buf[INET6_ADDRSTRLEN]; 3428 ipfw_insn *ret = NULL; 3429 int len; 3430 3431 /* Copy first address in set if needed */ 3432 if ((ch = strpbrk(av, "/,")) != NULL) { 3433 len = ch - av; 3434 strlcpy(buf, av, sizeof(buf)); 3435 if (len < sizeof(buf)) 3436 buf[len] = '\0'; 3437 host = buf; 3438 } else 3439 host = av; 3440 3441 if (proto == IPPROTO_IPV6 || strcmp(av, "me6") == 0 || 3442 inet_pton(AF_INET6, host, &a) == 1) 3443 ret = add_dstip6(cmd, av, cblen); 3444 /* XXX: should check for IPv4, not !IPv6 */ 3445 if (ret == NULL && (proto == IPPROTO_IP || strcmp(av, "me") == 0 || 3446 inet_pton(AF_INET6, host, &a) != 1)) 3447 ret = add_dstip(cmd, av, cblen, tstate); 3448 if (ret == NULL && strcmp(av, "any") != 0) 3449 ret = cmd; 3450 3451 return ret; 3452 } 3453 3454 /* 3455 * Parse arguments and assemble the microinstructions which make up a rule. 3456 * Rules are added into the 'rulebuf' and then copied in the correct order 3457 * into the actual rule. 3458 * 3459 * The syntax for a rule starts with the action, followed by 3460 * optional action parameters, and the various match patterns. 3461 * In the assembled microcode, the first opcode must be an O_PROBE_STATE 3462 * (generated if the rule includes a keep-state option), then the 3463 * various match patterns, log/altq actions, and the actual action. 3464 * 3465 */ 3466 void 3467 compile_rule(char *av[], uint32_t *rbuf, int *rbufsize, struct tidx *tstate) 3468 { 3469 /* 3470 * rules are added into the 'rulebuf' and then copied in 3471 * the correct order into the actual rule. 3472 * Some things that need to go out of order (prob, action etc.) 3473 * go into actbuf[]. 3474 */ 3475 static uint32_t actbuf[255], cmdbuf[255]; 3476 int rblen, ablen, cblen; 3477 3478 ipfw_insn *src, *dst, *cmd, *action, *prev=NULL; 3479 ipfw_insn *first_cmd; /* first match pattern */ 3480 3481 struct ip_fw_rule *rule; 3482 3483 /* 3484 * various flags used to record that we entered some fields. 3485 */ 3486 ipfw_insn *have_state = NULL; /* check-state or keep-state */ 3487 ipfw_insn *have_log = NULL, *have_altq = NULL, *have_tag = NULL; 3488 size_t len; 3489 3490 int i; 3491 3492 int open_par = 0; /* open parenthesis ( */ 3493 3494 /* proto is here because it is used to fetch ports */ 3495 u_char proto = IPPROTO_IP; /* default protocol */ 3496 3497 double match_prob = 1; /* match probability, default is always match */ 3498 3499 bzero(actbuf, sizeof(actbuf)); /* actions go here */ 3500 bzero(cmdbuf, sizeof(cmdbuf)); 3501 bzero(rbuf, *rbufsize); 3502 3503 rule = (struct ip_fw_rule *)rbuf; 3504 cmd = (ipfw_insn *)cmdbuf; 3505 action = (ipfw_insn *)actbuf; 3506 3507 rblen = *rbufsize / sizeof(uint32_t); 3508 rblen -= sizeof(struct ip_fw_rule) / sizeof(uint32_t); 3509 ablen = sizeof(actbuf) / sizeof(actbuf[0]); 3510 cblen = sizeof(cmdbuf) / sizeof(cmdbuf[0]); 3511 cblen -= F_INSN_SIZE(ipfw_insn_u32) + 1; 3512 3513 #define CHECK_RBUFLEN(len) { CHECK_LENGTH(rblen, len); rblen -= len; } 3514 #define CHECK_ACTLEN CHECK_LENGTH(ablen, action->len) 3515 3516 av++; 3517 3518 /* [rule N] -- Rule number optional */ 3519 if (av[0] && isdigit(**av)) { 3520 rule->rulenum = atoi(*av); 3521 av++; 3522 } 3523 3524 /* [set N] -- set number (0..RESVD_SET), optional */ 3525 if (av[0] && av[1] && _substrcmp(*av, "set") == 0) { 3526 int set = strtoul(av[1], NULL, 10); 3527 if (set < 0 || set > RESVD_SET) 3528 errx(EX_DATAERR, "illegal set %s", av[1]); 3529 rule->set = set; 3530 tstate->set = set; 3531 av += 2; 3532 } 3533 3534 /* [prob D] -- match probability, optional */ 3535 if (av[0] && av[1] && _substrcmp(*av, "prob") == 0) { 3536 match_prob = strtod(av[1], NULL); 3537 3538 if (match_prob <= 0 || match_prob > 1) 3539 errx(EX_DATAERR, "illegal match prob. %s", av[1]); 3540 av += 2; 3541 } 3542 3543 /* action -- mandatory */ 3544 NEED1("missing action"); 3545 i = match_token(rule_actions, *av); 3546 av++; 3547 action->len = 1; /* default */ 3548 CHECK_ACTLEN; 3549 switch(i) { 3550 case TOK_CHECKSTATE: 3551 have_state = action; 3552 action->opcode = O_CHECK_STATE; 3553 break; 3554 3555 case TOK_ACCEPT: 3556 action->opcode = O_ACCEPT; 3557 break; 3558 3559 case TOK_DENY: 3560 action->opcode = O_DENY; 3561 action->arg1 = 0; 3562 break; 3563 3564 case TOK_REJECT: 3565 action->opcode = O_REJECT; 3566 action->arg1 = ICMP_UNREACH_HOST; 3567 break; 3568 3569 case TOK_RESET: 3570 action->opcode = O_REJECT; 3571 action->arg1 = ICMP_REJECT_RST; 3572 break; 3573 3574 case TOK_RESET6: 3575 action->opcode = O_UNREACH6; 3576 action->arg1 = ICMP6_UNREACH_RST; 3577 break; 3578 3579 case TOK_UNREACH: 3580 action->opcode = O_REJECT; 3581 NEED1("missing reject code"); 3582 fill_reject_code(&action->arg1, *av); 3583 av++; 3584 break; 3585 3586 case TOK_UNREACH6: 3587 action->opcode = O_UNREACH6; 3588 NEED1("missing unreach code"); 3589 fill_unreach6_code(&action->arg1, *av); 3590 av++; 3591 break; 3592 3593 case TOK_COUNT: 3594 action->opcode = O_COUNT; 3595 break; 3596 3597 case TOK_NAT: 3598 action->opcode = O_NAT; 3599 action->len = F_INSN_SIZE(ipfw_insn_nat); 3600 CHECK_ACTLEN; 3601 if (_substrcmp(*av, "global") == 0) { 3602 action->arg1 = 0; 3603 av++; 3604 break; 3605 } else 3606 goto chkarg; 3607 3608 case TOK_QUEUE: 3609 action->opcode = O_QUEUE; 3610 goto chkarg; 3611 case TOK_PIPE: 3612 action->opcode = O_PIPE; 3613 goto chkarg; 3614 case TOK_SKIPTO: 3615 action->opcode = O_SKIPTO; 3616 goto chkarg; 3617 case TOK_NETGRAPH: 3618 action->opcode = O_NETGRAPH; 3619 goto chkarg; 3620 case TOK_NGTEE: 3621 action->opcode = O_NGTEE; 3622 goto chkarg; 3623 case TOK_DIVERT: 3624 action->opcode = O_DIVERT; 3625 goto chkarg; 3626 case TOK_TEE: 3627 action->opcode = O_TEE; 3628 goto chkarg; 3629 case TOK_CALL: 3630 action->opcode = O_CALLRETURN; 3631 chkarg: 3632 if (!av[0]) 3633 errx(EX_USAGE, "missing argument for %s", *(av - 1)); 3634 if (isdigit(**av)) { 3635 action->arg1 = strtoul(*av, NULL, 10); 3636 if (action->arg1 <= 0 || action->arg1 >= IP_FW_TABLEARG) 3637 errx(EX_DATAERR, "illegal argument for %s", 3638 *(av - 1)); 3639 } else if (_substrcmp(*av, "tablearg") == 0) { 3640 action->arg1 = IP_FW_TARG; 3641 } else if (i == TOK_DIVERT || i == TOK_TEE) { 3642 struct servent *s; 3643 setservent(1); 3644 s = getservbyname(av[0], "divert"); 3645 if (s != NULL) 3646 action->arg1 = ntohs(s->s_port); 3647 else 3648 errx(EX_DATAERR, "illegal divert/tee port"); 3649 } else 3650 errx(EX_DATAERR, "illegal argument for %s", *(av - 1)); 3651 av++; 3652 break; 3653 3654 case TOK_FORWARD: { 3655 /* 3656 * Locate the address-port separator (':' or ','). 3657 * Could be one of the following: 3658 * hostname:port 3659 * IPv4 a.b.c.d,port 3660 * IPv4 a.b.c.d:port 3661 * IPv6 w:x:y::z,port 3662 * The ':' can only be used with hostname and IPv4 address. 3663 * XXX-BZ Should we also support [w:x:y::z]:port? 3664 */ 3665 struct sockaddr_storage result; 3666 struct addrinfo *res; 3667 char *s, *end; 3668 int family; 3669 u_short port_number; 3670 3671 NEED1("missing forward address[:port]"); 3672 3673 /* 3674 * locate the address-port separator (':' or ',') 3675 */ 3676 s = strchr(*av, ','); 3677 if (s == NULL) { 3678 /* Distinguish between IPv4:port and IPv6 cases. */ 3679 s = strchr(*av, ':'); 3680 if (s && strchr(s+1, ':')) 3681 s = NULL; /* no port */ 3682 } 3683 3684 port_number = 0; 3685 if (s != NULL) { 3686 /* Terminate host portion and set s to start of port. */ 3687 *(s++) = '\0'; 3688 i = strtoport(s, &end, 0 /* base */, 0 /* proto */); 3689 if (s == end) 3690 errx(EX_DATAERR, 3691 "illegal forwarding port ``%s''", s); 3692 port_number = (u_short)i; 3693 } 3694 3695 if (_substrcmp(*av, "tablearg") == 0) { 3696 family = PF_INET; 3697 ((struct sockaddr_in*)&result)->sin_addr.s_addr = 3698 INADDR_ANY; 3699 } else { 3700 /* 3701 * Resolve the host name or address to a family and a 3702 * network representation of the address. 3703 */ 3704 if (getaddrinfo(*av, NULL, NULL, &res)) 3705 errx(EX_DATAERR, NULL); 3706 /* Just use the first host in the answer. */ 3707 family = res->ai_family; 3708 memcpy(&result, res->ai_addr, res->ai_addrlen); 3709 freeaddrinfo(res); 3710 } 3711 3712 if (family == PF_INET) { 3713 ipfw_insn_sa *p = (ipfw_insn_sa *)action; 3714 3715 action->opcode = O_FORWARD_IP; 3716 action->len = F_INSN_SIZE(ipfw_insn_sa); 3717 CHECK_ACTLEN; 3718 3719 /* 3720 * In the kernel we assume AF_INET and use only 3721 * sin_port and sin_addr. Remember to set sin_len as 3722 * the routing code seems to use it too. 3723 */ 3724 p->sa.sin_len = sizeof(struct sockaddr_in); 3725 p->sa.sin_family = AF_INET; 3726 p->sa.sin_port = port_number; 3727 p->sa.sin_addr.s_addr = 3728 ((struct sockaddr_in *)&result)->sin_addr.s_addr; 3729 } else if (family == PF_INET6) { 3730 ipfw_insn_sa6 *p = (ipfw_insn_sa6 *)action; 3731 3732 action->opcode = O_FORWARD_IP6; 3733 action->len = F_INSN_SIZE(ipfw_insn_sa6); 3734 CHECK_ACTLEN; 3735 3736 p->sa.sin6_len = sizeof(struct sockaddr_in6); 3737 p->sa.sin6_family = AF_INET6; 3738 p->sa.sin6_port = port_number; 3739 p->sa.sin6_flowinfo = 0; 3740 p->sa.sin6_scope_id = 0; 3741 /* No table support for v6 yet. */ 3742 bcopy(&((struct sockaddr_in6*)&result)->sin6_addr, 3743 &p->sa.sin6_addr, sizeof(p->sa.sin6_addr)); 3744 } else { 3745 errx(EX_DATAERR, "Invalid address family in forward action"); 3746 } 3747 av++; 3748 break; 3749 } 3750 case TOK_COMMENT: 3751 /* pretend it is a 'count' rule followed by the comment */ 3752 action->opcode = O_COUNT; 3753 av--; /* go back... */ 3754 break; 3755 3756 case TOK_SETFIB: 3757 { 3758 int numfibs; 3759 size_t intsize = sizeof(int); 3760 3761 action->opcode = O_SETFIB; 3762 NEED1("missing fib number"); 3763 if (_substrcmp(*av, "tablearg") == 0) { 3764 action->arg1 = IP_FW_TARG; 3765 } else { 3766 action->arg1 = strtoul(*av, NULL, 10); 3767 if (sysctlbyname("net.fibs", &numfibs, &intsize, 3768 NULL, 0) == -1) 3769 errx(EX_DATAERR, "fibs not suported.\n"); 3770 if (action->arg1 >= numfibs) /* Temporary */ 3771 errx(EX_DATAERR, "fib too large.\n"); 3772 /* Add high-order bit to fib to make room for tablearg*/ 3773 action->arg1 |= 0x8000; 3774 } 3775 av++; 3776 break; 3777 } 3778 3779 case TOK_SETDSCP: 3780 { 3781 int code; 3782 3783 action->opcode = O_SETDSCP; 3784 NEED1("missing DSCP code"); 3785 if (_substrcmp(*av, "tablearg") == 0) { 3786 action->arg1 = IP_FW_TARG; 3787 } else if (isalpha(*av[0])) { 3788 if ((code = match_token(f_ipdscp, *av)) == -1) 3789 errx(EX_DATAERR, "Unknown DSCP code"); 3790 action->arg1 = code; 3791 } else 3792 action->arg1 = strtoul(*av, NULL, 10); 3793 /* Add high-order bit to DSCP to make room for tablearg */ 3794 if (action->arg1 != IP_FW_TARG) 3795 action->arg1 |= 0x8000; 3796 av++; 3797 break; 3798 } 3799 3800 case TOK_REASS: 3801 action->opcode = O_REASS; 3802 break; 3803 3804 case TOK_RETURN: 3805 fill_cmd(action, O_CALLRETURN, F_NOT, 0); 3806 break; 3807 3808 default: 3809 errx(EX_DATAERR, "invalid action %s\n", av[-1]); 3810 } 3811 action = next_cmd(action, &ablen); 3812 3813 /* 3814 * [altq queuename] -- altq tag, optional 3815 * [log [logamount N]] -- log, optional 3816 * 3817 * If they exist, it go first in the cmdbuf, but then it is 3818 * skipped in the copy section to the end of the buffer. 3819 */ 3820 while (av[0] != NULL && (i = match_token(rule_action_params, *av)) != -1) { 3821 av++; 3822 switch (i) { 3823 case TOK_LOG: 3824 { 3825 ipfw_insn_log *c = (ipfw_insn_log *)cmd; 3826 int l; 3827 3828 if (have_log) 3829 errx(EX_DATAERR, 3830 "log cannot be specified more than once"); 3831 have_log = (ipfw_insn *)c; 3832 cmd->len = F_INSN_SIZE(ipfw_insn_log); 3833 CHECK_CMDLEN; 3834 cmd->opcode = O_LOG; 3835 if (av[0] && _substrcmp(*av, "logamount") == 0) { 3836 av++; 3837 NEED1("logamount requires argument"); 3838 l = atoi(*av); 3839 if (l < 0) 3840 errx(EX_DATAERR, 3841 "logamount must be positive"); 3842 c->max_log = l; 3843 av++; 3844 } else { 3845 len = sizeof(c->max_log); 3846 if (sysctlbyname("net.inet.ip.fw.verbose_limit", 3847 &c->max_log, &len, NULL, 0) == -1) { 3848 if (co.test_only) { 3849 c->max_log = 0; 3850 break; 3851 } 3852 errx(1, "sysctlbyname(\"%s\")", 3853 "net.inet.ip.fw.verbose_limit"); 3854 } 3855 } 3856 } 3857 break; 3858 3859 #ifndef NO_ALTQ 3860 case TOK_ALTQ: 3861 { 3862 ipfw_insn_altq *a = (ipfw_insn_altq *)cmd; 3863 3864 NEED1("missing altq queue name"); 3865 if (have_altq) 3866 errx(EX_DATAERR, 3867 "altq cannot be specified more than once"); 3868 have_altq = (ipfw_insn *)a; 3869 cmd->len = F_INSN_SIZE(ipfw_insn_altq); 3870 CHECK_CMDLEN; 3871 cmd->opcode = O_ALTQ; 3872 a->qid = altq_name_to_qid(*av); 3873 av++; 3874 } 3875 break; 3876 #endif 3877 3878 case TOK_TAG: 3879 case TOK_UNTAG: { 3880 uint16_t tag; 3881 3882 if (have_tag) 3883 errx(EX_USAGE, "tag and untag cannot be " 3884 "specified more than once"); 3885 GET_UINT_ARG(tag, IPFW_ARG_MIN, IPFW_ARG_MAX, i, 3886 rule_action_params); 3887 have_tag = cmd; 3888 fill_cmd(cmd, O_TAG, (i == TOK_TAG) ? 0: F_NOT, tag); 3889 av++; 3890 break; 3891 } 3892 3893 default: 3894 abort(); 3895 } 3896 cmd = next_cmd(cmd, &cblen); 3897 } 3898 3899 if (have_state) /* must be a check-state, we are done */ 3900 goto done; 3901 3902 #define OR_START(target) \ 3903 if (av[0] && (*av[0] == '(' || *av[0] == '{')) { \ 3904 if (open_par) \ 3905 errx(EX_USAGE, "nested \"(\" not allowed\n"); \ 3906 prev = NULL; \ 3907 open_par = 1; \ 3908 if ( (av[0])[1] == '\0') { \ 3909 av++; \ 3910 } else \ 3911 (*av)++; \ 3912 } \ 3913 target: \ 3914 3915 3916 #define CLOSE_PAR \ 3917 if (open_par) { \ 3918 if (av[0] && ( \ 3919 strcmp(*av, ")") == 0 || \ 3920 strcmp(*av, "}") == 0)) { \ 3921 prev = NULL; \ 3922 open_par = 0; \ 3923 av++; \ 3924 } else \ 3925 errx(EX_USAGE, "missing \")\"\n"); \ 3926 } 3927 3928 #define NOT_BLOCK \ 3929 if (av[0] && _substrcmp(*av, "not") == 0) { \ 3930 if (cmd->len & F_NOT) \ 3931 errx(EX_USAGE, "double \"not\" not allowed\n"); \ 3932 cmd->len |= F_NOT; \ 3933 av++; \ 3934 } 3935 3936 #define OR_BLOCK(target) \ 3937 if (av[0] && _substrcmp(*av, "or") == 0) { \ 3938 if (prev == NULL || open_par == 0) \ 3939 errx(EX_DATAERR, "invalid OR block"); \ 3940 prev->len |= F_OR; \ 3941 av++; \ 3942 goto target; \ 3943 } \ 3944 CLOSE_PAR; 3945 3946 first_cmd = cmd; 3947 3948 #if 0 3949 /* 3950 * MAC addresses, optional. 3951 * If we have this, we skip the part "proto from src to dst" 3952 * and jump straight to the option parsing. 3953 */ 3954 NOT_BLOCK; 3955 NEED1("missing protocol"); 3956 if (_substrcmp(*av, "MAC") == 0 || 3957 _substrcmp(*av, "mac") == 0) { 3958 av++; /* the "MAC" keyword */ 3959 add_mac(cmd, av); /* exits in case of errors */ 3960 cmd = next_cmd(cmd); 3961 av += 2; /* dst-mac and src-mac */ 3962 NOT_BLOCK; 3963 NEED1("missing mac type"); 3964 if (add_mactype(cmd, av[0])) 3965 cmd = next_cmd(cmd); 3966 av++; /* any or mac-type */ 3967 goto read_options; 3968 } 3969 #endif 3970 3971 /* 3972 * protocol, mandatory 3973 */ 3974 OR_START(get_proto); 3975 NOT_BLOCK; 3976 NEED1("missing protocol"); 3977 if (add_proto_compat(cmd, *av, &proto)) { 3978 av++; 3979 if (F_LEN(cmd) != 0) { 3980 prev = cmd; 3981 cmd = next_cmd(cmd, &cblen); 3982 } 3983 } else if (first_cmd != cmd) { 3984 errx(EX_DATAERR, "invalid protocol ``%s''", *av); 3985 } else 3986 goto read_options; 3987 OR_BLOCK(get_proto); 3988 3989 /* 3990 * "from", mandatory 3991 */ 3992 if ((av[0] == NULL) || _substrcmp(*av, "from") != 0) 3993 errx(EX_USAGE, "missing ``from''"); 3994 av++; 3995 3996 /* 3997 * source IP, mandatory 3998 */ 3999 OR_START(source_ip); 4000 NOT_BLOCK; /* optional "not" */ 4001 NEED1("missing source address"); 4002 if (add_src(cmd, *av, proto, cblen, tstate)) { 4003 av++; 4004 if (F_LEN(cmd) != 0) { /* ! any */ 4005 prev = cmd; 4006 cmd = next_cmd(cmd, &cblen); 4007 } 4008 } else 4009 errx(EX_USAGE, "bad source address %s", *av); 4010 OR_BLOCK(source_ip); 4011 4012 /* 4013 * source ports, optional 4014 */ 4015 NOT_BLOCK; /* optional "not" */ 4016 if ( av[0] != NULL ) { 4017 if (_substrcmp(*av, "any") == 0 || 4018 add_ports(cmd, *av, proto, O_IP_SRCPORT, cblen)) { 4019 av++; 4020 if (F_LEN(cmd) != 0) 4021 cmd = next_cmd(cmd, &cblen); 4022 } 4023 } 4024 4025 /* 4026 * "to", mandatory 4027 */ 4028 if ( (av[0] == NULL) || _substrcmp(*av, "to") != 0 ) 4029 errx(EX_USAGE, "missing ``to''"); 4030 av++; 4031 4032 /* 4033 * destination, mandatory 4034 */ 4035 OR_START(dest_ip); 4036 NOT_BLOCK; /* optional "not" */ 4037 NEED1("missing dst address"); 4038 if (add_dst(cmd, *av, proto, cblen, tstate)) { 4039 av++; 4040 if (F_LEN(cmd) != 0) { /* ! any */ 4041 prev = cmd; 4042 cmd = next_cmd(cmd, &cblen); 4043 } 4044 } else 4045 errx( EX_USAGE, "bad destination address %s", *av); 4046 OR_BLOCK(dest_ip); 4047 4048 /* 4049 * dest. ports, optional 4050 */ 4051 NOT_BLOCK; /* optional "not" */ 4052 if (av[0]) { 4053 if (_substrcmp(*av, "any") == 0 || 4054 add_ports(cmd, *av, proto, O_IP_DSTPORT, cblen)) { 4055 av++; 4056 if (F_LEN(cmd) != 0) 4057 cmd = next_cmd(cmd, &cblen); 4058 } 4059 } 4060 4061 read_options: 4062 if (av[0] && first_cmd == cmd) { 4063 /* 4064 * nothing specified so far, store in the rule to ease 4065 * printout later. 4066 */ 4067 rule->flags |= IPFW_RULE_NOOPT; 4068 } 4069 prev = NULL; 4070 while ( av[0] != NULL ) { 4071 char *s; 4072 ipfw_insn_u32 *cmd32; /* alias for cmd */ 4073 4074 s = *av; 4075 cmd32 = (ipfw_insn_u32 *)cmd; 4076 4077 if (*s == '!') { /* alternate syntax for NOT */ 4078 if (cmd->len & F_NOT) 4079 errx(EX_USAGE, "double \"not\" not allowed\n"); 4080 cmd->len = F_NOT; 4081 s++; 4082 } 4083 i = match_token(rule_options, s); 4084 av++; 4085 switch(i) { 4086 case TOK_NOT: 4087 if (cmd->len & F_NOT) 4088 errx(EX_USAGE, "double \"not\" not allowed\n"); 4089 cmd->len = F_NOT; 4090 break; 4091 4092 case TOK_OR: 4093 if (open_par == 0 || prev == NULL) 4094 errx(EX_USAGE, "invalid \"or\" block\n"); 4095 prev->len |= F_OR; 4096 break; 4097 4098 case TOK_STARTBRACE: 4099 if (open_par) 4100 errx(EX_USAGE, "+nested \"(\" not allowed\n"); 4101 open_par = 1; 4102 break; 4103 4104 case TOK_ENDBRACE: 4105 if (!open_par) 4106 errx(EX_USAGE, "+missing \")\"\n"); 4107 open_par = 0; 4108 prev = NULL; 4109 break; 4110 4111 case TOK_IN: 4112 fill_cmd(cmd, O_IN, 0, 0); 4113 break; 4114 4115 case TOK_OUT: 4116 cmd->len ^= F_NOT; /* toggle F_NOT */ 4117 fill_cmd(cmd, O_IN, 0, 0); 4118 break; 4119 4120 case TOK_DIVERTED: 4121 fill_cmd(cmd, O_DIVERTED, 0, 3); 4122 break; 4123 4124 case TOK_DIVERTEDLOOPBACK: 4125 fill_cmd(cmd, O_DIVERTED, 0, 1); 4126 break; 4127 4128 case TOK_DIVERTEDOUTPUT: 4129 fill_cmd(cmd, O_DIVERTED, 0, 2); 4130 break; 4131 4132 case TOK_FRAG: 4133 fill_cmd(cmd, O_FRAG, 0, 0); 4134 break; 4135 4136 case TOK_LAYER2: 4137 fill_cmd(cmd, O_LAYER2, 0, 0); 4138 break; 4139 4140 case TOK_XMIT: 4141 case TOK_RECV: 4142 case TOK_VIA: 4143 NEED1("recv, xmit, via require interface name" 4144 " or address"); 4145 fill_iface((ipfw_insn_if *)cmd, av[0], cblen, tstate); 4146 av++; 4147 if (F_LEN(cmd) == 0) /* not a valid address */ 4148 break; 4149 if (i == TOK_XMIT) 4150 cmd->opcode = O_XMIT; 4151 else if (i == TOK_RECV) 4152 cmd->opcode = O_RECV; 4153 else if (i == TOK_VIA) 4154 cmd->opcode = O_VIA; 4155 break; 4156 4157 case TOK_ICMPTYPES: 4158 NEED1("icmptypes requires list of types"); 4159 fill_icmptypes((ipfw_insn_u32 *)cmd, *av); 4160 av++; 4161 break; 4162 4163 case TOK_ICMP6TYPES: 4164 NEED1("icmptypes requires list of types"); 4165 fill_icmp6types((ipfw_insn_icmp6 *)cmd, *av, cblen); 4166 av++; 4167 break; 4168 4169 case TOK_IPTTL: 4170 NEED1("ipttl requires TTL"); 4171 if (strpbrk(*av, "-,")) { 4172 if (!add_ports(cmd, *av, 0, O_IPTTL, cblen)) 4173 errx(EX_DATAERR, "invalid ipttl %s", *av); 4174 } else 4175 fill_cmd(cmd, O_IPTTL, 0, strtoul(*av, NULL, 0)); 4176 av++; 4177 break; 4178 4179 case TOK_IPID: 4180 NEED1("ipid requires id"); 4181 if (strpbrk(*av, "-,")) { 4182 if (!add_ports(cmd, *av, 0, O_IPID, cblen)) 4183 errx(EX_DATAERR, "invalid ipid %s", *av); 4184 } else 4185 fill_cmd(cmd, O_IPID, 0, strtoul(*av, NULL, 0)); 4186 av++; 4187 break; 4188 4189 case TOK_IPLEN: 4190 NEED1("iplen requires length"); 4191 if (strpbrk(*av, "-,")) { 4192 if (!add_ports(cmd, *av, 0, O_IPLEN, cblen)) 4193 errx(EX_DATAERR, "invalid ip len %s", *av); 4194 } else 4195 fill_cmd(cmd, O_IPLEN, 0, strtoul(*av, NULL, 0)); 4196 av++; 4197 break; 4198 4199 case TOK_IPVER: 4200 NEED1("ipver requires version"); 4201 fill_cmd(cmd, O_IPVER, 0, strtoul(*av, NULL, 0)); 4202 av++; 4203 break; 4204 4205 case TOK_IPPRECEDENCE: 4206 NEED1("ipprecedence requires value"); 4207 fill_cmd(cmd, O_IPPRECEDENCE, 0, 4208 (strtoul(*av, NULL, 0) & 7) << 5); 4209 av++; 4210 break; 4211 4212 case TOK_DSCP: 4213 NEED1("missing DSCP code"); 4214 fill_dscp(cmd, *av, cblen); 4215 av++; 4216 break; 4217 4218 case TOK_IPOPTS: 4219 NEED1("missing argument for ipoptions"); 4220 fill_flags_cmd(cmd, O_IPOPT, f_ipopts, *av); 4221 av++; 4222 break; 4223 4224 case TOK_IPTOS: 4225 NEED1("missing argument for iptos"); 4226 fill_flags_cmd(cmd, O_IPTOS, f_iptos, *av); 4227 av++; 4228 break; 4229 4230 case TOK_UID: 4231 NEED1("uid requires argument"); 4232 { 4233 char *end; 4234 uid_t uid; 4235 struct passwd *pwd; 4236 4237 cmd->opcode = O_UID; 4238 uid = strtoul(*av, &end, 0); 4239 pwd = (*end == '\0') ? getpwuid(uid) : getpwnam(*av); 4240 if (pwd == NULL) 4241 errx(EX_DATAERR, "uid \"%s\" nonexistent", *av); 4242 cmd32->d[0] = pwd->pw_uid; 4243 cmd->len |= F_INSN_SIZE(ipfw_insn_u32); 4244 av++; 4245 } 4246 break; 4247 4248 case TOK_GID: 4249 NEED1("gid requires argument"); 4250 { 4251 char *end; 4252 gid_t gid; 4253 struct group *grp; 4254 4255 cmd->opcode = O_GID; 4256 gid = strtoul(*av, &end, 0); 4257 grp = (*end == '\0') ? getgrgid(gid) : getgrnam(*av); 4258 if (grp == NULL) 4259 errx(EX_DATAERR, "gid \"%s\" nonexistent", *av); 4260 cmd32->d[0] = grp->gr_gid; 4261 cmd->len |= F_INSN_SIZE(ipfw_insn_u32); 4262 av++; 4263 } 4264 break; 4265 4266 case TOK_JAIL: 4267 NEED1("jail requires argument"); 4268 { 4269 char *end; 4270 int jid; 4271 4272 cmd->opcode = O_JAIL; 4273 jid = (int)strtol(*av, &end, 0); 4274 if (jid < 0 || *end != '\0') 4275 errx(EX_DATAERR, "jail requires prison ID"); 4276 cmd32->d[0] = (uint32_t)jid; 4277 cmd->len |= F_INSN_SIZE(ipfw_insn_u32); 4278 av++; 4279 } 4280 break; 4281 4282 case TOK_ESTAB: 4283 fill_cmd(cmd, O_ESTAB, 0, 0); 4284 break; 4285 4286 case TOK_SETUP: 4287 fill_cmd(cmd, O_TCPFLAGS, 0, 4288 (TH_SYN) | ( (TH_ACK) & 0xff) <<8 ); 4289 break; 4290 4291 case TOK_TCPDATALEN: 4292 NEED1("tcpdatalen requires length"); 4293 if (strpbrk(*av, "-,")) { 4294 if (!add_ports(cmd, *av, 0, O_TCPDATALEN, cblen)) 4295 errx(EX_DATAERR, "invalid tcpdata len %s", *av); 4296 } else 4297 fill_cmd(cmd, O_TCPDATALEN, 0, 4298 strtoul(*av, NULL, 0)); 4299 av++; 4300 break; 4301 4302 case TOK_TCPOPTS: 4303 NEED1("missing argument for tcpoptions"); 4304 fill_flags_cmd(cmd, O_TCPOPTS, f_tcpopts, *av); 4305 av++; 4306 break; 4307 4308 case TOK_TCPSEQ: 4309 case TOK_TCPACK: 4310 NEED1("tcpseq/tcpack requires argument"); 4311 cmd->len = F_INSN_SIZE(ipfw_insn_u32); 4312 cmd->opcode = (i == TOK_TCPSEQ) ? O_TCPSEQ : O_TCPACK; 4313 cmd32->d[0] = htonl(strtoul(*av, NULL, 0)); 4314 av++; 4315 break; 4316 4317 case TOK_TCPWIN: 4318 NEED1("tcpwin requires length"); 4319 if (strpbrk(*av, "-,")) { 4320 if (!add_ports(cmd, *av, 0, O_TCPWIN, cblen)) 4321 errx(EX_DATAERR, "invalid tcpwin len %s", *av); 4322 } else 4323 fill_cmd(cmd, O_TCPWIN, 0, 4324 strtoul(*av, NULL, 0)); 4325 av++; 4326 break; 4327 4328 case TOK_TCPFLAGS: 4329 NEED1("missing argument for tcpflags"); 4330 cmd->opcode = O_TCPFLAGS; 4331 fill_flags_cmd(cmd, O_TCPFLAGS, f_tcpflags, *av); 4332 av++; 4333 break; 4334 4335 case TOK_KEEPSTATE: 4336 if (open_par) 4337 errx(EX_USAGE, "keep-state cannot be part " 4338 "of an or block"); 4339 if (have_state) 4340 errx(EX_USAGE, "only one of keep-state " 4341 "and limit is allowed"); 4342 have_state = cmd; 4343 fill_cmd(cmd, O_KEEP_STATE, 0, 0); 4344 break; 4345 4346 case TOK_LIMIT: { 4347 ipfw_insn_limit *c = (ipfw_insn_limit *)cmd; 4348 int val; 4349 4350 if (open_par) 4351 errx(EX_USAGE, 4352 "limit cannot be part of an or block"); 4353 if (have_state) 4354 errx(EX_USAGE, "only one of keep-state and " 4355 "limit is allowed"); 4356 have_state = cmd; 4357 4358 cmd->len = F_INSN_SIZE(ipfw_insn_limit); 4359 CHECK_CMDLEN; 4360 cmd->opcode = O_LIMIT; 4361 c->limit_mask = c->conn_limit = 0; 4362 4363 while ( av[0] != NULL ) { 4364 if ((val = match_token(limit_masks, *av)) <= 0) 4365 break; 4366 c->limit_mask |= val; 4367 av++; 4368 } 4369 4370 if (c->limit_mask == 0) 4371 errx(EX_USAGE, "limit: missing limit mask"); 4372 4373 GET_UINT_ARG(c->conn_limit, IPFW_ARG_MIN, IPFW_ARG_MAX, 4374 TOK_LIMIT, rule_options); 4375 4376 av++; 4377 break; 4378 } 4379 4380 case TOK_PROTO: 4381 NEED1("missing protocol"); 4382 if (add_proto(cmd, *av, &proto)) { 4383 av++; 4384 } else 4385 errx(EX_DATAERR, "invalid protocol ``%s''", 4386 *av); 4387 break; 4388 4389 case TOK_SRCIP: 4390 NEED1("missing source IP"); 4391 if (add_srcip(cmd, *av, cblen, tstate)) { 4392 av++; 4393 } 4394 break; 4395 4396 case TOK_DSTIP: 4397 NEED1("missing destination IP"); 4398 if (add_dstip(cmd, *av, cblen, tstate)) { 4399 av++; 4400 } 4401 break; 4402 4403 case TOK_SRCIP6: 4404 NEED1("missing source IP6"); 4405 if (add_srcip6(cmd, *av, cblen)) { 4406 av++; 4407 } 4408 break; 4409 4410 case TOK_DSTIP6: 4411 NEED1("missing destination IP6"); 4412 if (add_dstip6(cmd, *av, cblen)) { 4413 av++; 4414 } 4415 break; 4416 4417 case TOK_SRCPORT: 4418 NEED1("missing source port"); 4419 if (_substrcmp(*av, "any") == 0 || 4420 add_ports(cmd, *av, proto, O_IP_SRCPORT, cblen)) { 4421 av++; 4422 } else 4423 errx(EX_DATAERR, "invalid source port %s", *av); 4424 break; 4425 4426 case TOK_DSTPORT: 4427 NEED1("missing destination port"); 4428 if (_substrcmp(*av, "any") == 0 || 4429 add_ports(cmd, *av, proto, O_IP_DSTPORT, cblen)) { 4430 av++; 4431 } else 4432 errx(EX_DATAERR, "invalid destination port %s", 4433 *av); 4434 break; 4435 4436 case TOK_MAC: 4437 if (add_mac(cmd, av, cblen)) 4438 av += 2; 4439 break; 4440 4441 case TOK_MACTYPE: 4442 NEED1("missing mac type"); 4443 if (!add_mactype(cmd, *av, cblen)) 4444 errx(EX_DATAERR, "invalid mac type %s", *av); 4445 av++; 4446 break; 4447 4448 case TOK_VERREVPATH: 4449 fill_cmd(cmd, O_VERREVPATH, 0, 0); 4450 break; 4451 4452 case TOK_VERSRCREACH: 4453 fill_cmd(cmd, O_VERSRCREACH, 0, 0); 4454 break; 4455 4456 case TOK_ANTISPOOF: 4457 fill_cmd(cmd, O_ANTISPOOF, 0, 0); 4458 break; 4459 4460 case TOK_IPSEC: 4461 fill_cmd(cmd, O_IPSEC, 0, 0); 4462 break; 4463 4464 case TOK_IPV6: 4465 fill_cmd(cmd, O_IP6, 0, 0); 4466 break; 4467 4468 case TOK_IPV4: 4469 fill_cmd(cmd, O_IP4, 0, 0); 4470 break; 4471 4472 case TOK_EXT6HDR: 4473 fill_ext6hdr( cmd, *av ); 4474 av++; 4475 break; 4476 4477 case TOK_FLOWID: 4478 if (proto != IPPROTO_IPV6 ) 4479 errx( EX_USAGE, "flow-id filter is active " 4480 "only for ipv6 protocol\n"); 4481 fill_flow6( (ipfw_insn_u32 *) cmd, *av, cblen); 4482 av++; 4483 break; 4484 4485 case TOK_COMMENT: 4486 fill_comment(cmd, av, cblen); 4487 av[0]=NULL; 4488 break; 4489 4490 case TOK_TAGGED: 4491 if (av[0] && strpbrk(*av, "-,")) { 4492 if (!add_ports(cmd, *av, 0, O_TAGGED, cblen)) 4493 errx(EX_DATAERR, "tagged: invalid tag" 4494 " list: %s", *av); 4495 } 4496 else { 4497 uint16_t tag; 4498 4499 GET_UINT_ARG(tag, IPFW_ARG_MIN, IPFW_ARG_MAX, 4500 TOK_TAGGED, rule_options); 4501 fill_cmd(cmd, O_TAGGED, 0, tag); 4502 } 4503 av++; 4504 break; 4505 4506 case TOK_FIB: 4507 NEED1("fib requires fib number"); 4508 fill_cmd(cmd, O_FIB, 0, strtoul(*av, NULL, 0)); 4509 av++; 4510 break; 4511 case TOK_SOCKARG: 4512 fill_cmd(cmd, O_SOCKARG, 0, 0); 4513 break; 4514 4515 case TOK_LOOKUP: { 4516 ipfw_insn_u32 *c = (ipfw_insn_u32 *)cmd; 4517 int j; 4518 4519 if (!av[0] || !av[1]) 4520 errx(EX_USAGE, "format: lookup argument tablenum"); 4521 cmd->opcode = O_IP_DST_LOOKUP; 4522 cmd->len |= F_INSN_SIZE(ipfw_insn) + 2; 4523 i = match_token(rule_options, *av); 4524 for (j = 0; lookup_key[j] >= 0 ; j++) { 4525 if (i == lookup_key[j]) 4526 break; 4527 } 4528 if (lookup_key[j] <= 0) 4529 errx(EX_USAGE, "format: cannot lookup on %s", *av); 4530 __PAST_END(c->d, 1) = j; // i converted to option 4531 av++; 4532 4533 if ((j = pack_table(tstate, *av)) == 0) 4534 errx(EX_DATAERR, "Invalid table name: %s", *av); 4535 4536 cmd->arg1 = j; 4537 av++; 4538 } 4539 break; 4540 case TOK_FLOW: 4541 NEED1("missing table name"); 4542 if (strncmp(*av, "table(", 6) != 0) 4543 errx(EX_DATAERR, 4544 "enclose table name into \"table()\""); 4545 fill_table(cmd, *av, O_IP_FLOW_LOOKUP, tstate); 4546 av++; 4547 break; 4548 4549 default: 4550 errx(EX_USAGE, "unrecognised option [%d] %s\n", i, s); 4551 } 4552 if (F_LEN(cmd) > 0) { /* prepare to advance */ 4553 prev = cmd; 4554 cmd = next_cmd(cmd, &cblen); 4555 } 4556 } 4557 4558 done: 4559 /* 4560 * Now copy stuff into the rule. 4561 * If we have a keep-state option, the first instruction 4562 * must be a PROBE_STATE (which is generated here). 4563 * If we have a LOG option, it was stored as the first command, 4564 * and now must be moved to the top of the action part. 4565 */ 4566 dst = (ipfw_insn *)rule->cmd; 4567 4568 /* 4569 * First thing to write into the command stream is the match probability. 4570 */ 4571 if (match_prob != 1) { /* 1 means always match */ 4572 dst->opcode = O_PROB; 4573 dst->len = 2; 4574 *((int32_t *)(dst+1)) = (int32_t)(match_prob * 0x7fffffff); 4575 dst += dst->len; 4576 } 4577 4578 /* 4579 * generate O_PROBE_STATE if necessary 4580 */ 4581 if (have_state && have_state->opcode != O_CHECK_STATE) { 4582 fill_cmd(dst, O_PROBE_STATE, 0, 0); 4583 dst = next_cmd(dst, &rblen); 4584 } 4585 4586 /* copy all commands but O_LOG, O_KEEP_STATE, O_LIMIT, O_ALTQ, O_TAG */ 4587 for (src = (ipfw_insn *)cmdbuf; src != cmd; src += i) { 4588 i = F_LEN(src); 4589 CHECK_RBUFLEN(i); 4590 4591 switch (src->opcode) { 4592 case O_LOG: 4593 case O_KEEP_STATE: 4594 case O_LIMIT: 4595 case O_ALTQ: 4596 case O_TAG: 4597 break; 4598 default: 4599 bcopy(src, dst, i * sizeof(uint32_t)); 4600 dst += i; 4601 } 4602 } 4603 4604 /* 4605 * put back the have_state command as last opcode 4606 */ 4607 if (have_state && have_state->opcode != O_CHECK_STATE) { 4608 i = F_LEN(have_state); 4609 CHECK_RBUFLEN(i); 4610 bcopy(have_state, dst, i * sizeof(uint32_t)); 4611 dst += i; 4612 } 4613 /* 4614 * start action section 4615 */ 4616 rule->act_ofs = dst - rule->cmd; 4617 4618 /* put back O_LOG, O_ALTQ, O_TAG if necessary */ 4619 if (have_log) { 4620 i = F_LEN(have_log); 4621 CHECK_RBUFLEN(i); 4622 bcopy(have_log, dst, i * sizeof(uint32_t)); 4623 dst += i; 4624 } 4625 if (have_altq) { 4626 i = F_LEN(have_altq); 4627 CHECK_RBUFLEN(i); 4628 bcopy(have_altq, dst, i * sizeof(uint32_t)); 4629 dst += i; 4630 } 4631 if (have_tag) { 4632 i = F_LEN(have_tag); 4633 CHECK_RBUFLEN(i); 4634 bcopy(have_tag, dst, i * sizeof(uint32_t)); 4635 dst += i; 4636 } 4637 4638 /* 4639 * copy all other actions 4640 */ 4641 for (src = (ipfw_insn *)actbuf; src != action; src += i) { 4642 i = F_LEN(src); 4643 CHECK_RBUFLEN(i); 4644 bcopy(src, dst, i * sizeof(uint32_t)); 4645 dst += i; 4646 } 4647 4648 rule->cmd_len = (uint32_t *)dst - (uint32_t *)(rule->cmd); 4649 *rbufsize = (char *)dst - (char *)rule; 4650 } 4651 4652 /* 4653 * Adds one or more rules to ipfw chain. 4654 * Data layout: 4655 * Request: 4656 * [ 4657 * ip_fw3_opheader 4658 * [ ipfw_obj_ctlv(IPFW_TLV_TBL_LIST) ipfw_obj_ntlv x N ] (optional *1) 4659 * [ ipfw_obj_ctlv(IPFW_TLV_RULE_LIST) [ ip_fw_rule ip_fw_insn ] x N ] (*2) (*3) 4660 * ] 4661 * Reply: 4662 * [ 4663 * ip_fw3_opheader 4664 * [ ipfw_obj_ctlv(IPFW_TLV_TBL_LIST) ipfw_obj_ntlv x N ] (optional) 4665 * [ ipfw_obj_ctlv(IPFW_TLV_RULE_LIST) [ ip_fw_rule ip_fw_insn ] x N ] 4666 * ] 4667 * 4668 * Rules in reply are modified to store their actual ruleset number. 4669 * 4670 * (*1) TLVs inside IPFW_TLV_TBL_LIST needs to be sorted ascending 4671 * accoring to their idx field and there has to be no duplicates. 4672 * (*2) Numbered rules inside IPFW_TLV_RULE_LIST needs to be sorted ascending. 4673 * (*3) Each ip_fw structure needs to be aligned to u64 boundary. 4674 */ 4675 void 4676 ipfw_add(char *av[]) 4677 { 4678 uint32_t rulebuf[1024]; 4679 int rbufsize, default_off, tlen, rlen; 4680 size_t sz; 4681 struct tidx ts; 4682 struct ip_fw_rule *rule; 4683 caddr_t tbuf; 4684 ip_fw3_opheader *op3; 4685 ipfw_obj_ctlv *ctlv, *tstate; 4686 4687 rbufsize = sizeof(rulebuf); 4688 memset(&ts, 0, sizeof(ts)); 4689 4690 /* Optimize case with no tables */ 4691 default_off = sizeof(ipfw_obj_ctlv) + sizeof(ip_fw3_opheader); 4692 op3 = (ip_fw3_opheader *)rulebuf; 4693 ctlv = (ipfw_obj_ctlv *)(op3 + 1); 4694 rule = (struct ip_fw_rule *)(ctlv + 1); 4695 rbufsize -= default_off; 4696 4697 compile_rule(av, (uint32_t *)rule, &rbufsize, &ts); 4698 /* Align rule size to u64 boundary */ 4699 rlen = roundup2(rbufsize, sizeof(uint64_t)); 4700 4701 tbuf = NULL; 4702 sz = 0; 4703 tstate = NULL; 4704 if (ts.count != 0) { 4705 /* Some tables. We have to alloc more data */ 4706 tlen = ts.count * sizeof(ipfw_obj_ntlv); 4707 sz = default_off + sizeof(ipfw_obj_ctlv) + tlen + rlen; 4708 4709 if ((tbuf = calloc(1, sz)) == NULL) 4710 err(EX_UNAVAILABLE, "malloc() failed for IP_FW_ADD"); 4711 op3 = (ip_fw3_opheader *)tbuf; 4712 /* Tables first */ 4713 ctlv = (ipfw_obj_ctlv *)(op3 + 1); 4714 ctlv->head.type = IPFW_TLV_TBLNAME_LIST; 4715 ctlv->head.length = sizeof(ipfw_obj_ctlv) + tlen; 4716 ctlv->count = ts.count; 4717 ctlv->objsize = sizeof(ipfw_obj_ntlv); 4718 memcpy(ctlv + 1, ts.idx, tlen); 4719 table_sort_ctlv(ctlv); 4720 tstate = ctlv; 4721 /* Rule next */ 4722 ctlv = (ipfw_obj_ctlv *)((caddr_t)ctlv + ctlv->head.length); 4723 ctlv->head.type = IPFW_TLV_RULE_LIST; 4724 ctlv->head.length = sizeof(ipfw_obj_ctlv) + rlen; 4725 ctlv->count = 1; 4726 memcpy(ctlv + 1, rule, rbufsize); 4727 } else { 4728 /* Simply add header */ 4729 sz = rlen + default_off; 4730 memset(ctlv, 0, sizeof(*ctlv)); 4731 ctlv->head.type = IPFW_TLV_RULE_LIST; 4732 ctlv->head.length = sizeof(ipfw_obj_ctlv) + rlen; 4733 ctlv->count = 1; 4734 } 4735 4736 if (do_get3(IP_FW_XADD, op3, &sz) != 0) 4737 err(EX_UNAVAILABLE, "getsockopt(%s)", "IP_FW_XADD"); 4738 4739 if (!co.do_quiet) { 4740 struct format_opts sfo; 4741 struct buf_pr bp; 4742 memset(&sfo, 0, sizeof(sfo)); 4743 sfo.tstate = tstate; 4744 sfo.set_mask = (uint32_t)(-1); 4745 bp_alloc(&bp, 4096); 4746 show_static_rule(&co, &sfo, &bp, rule, NULL); 4747 printf("%s", bp.buf); 4748 bp_free(&bp); 4749 } 4750 4751 if (tbuf != NULL) 4752 free(tbuf); 4753 4754 if (ts.idx != NULL) 4755 free(ts.idx); 4756 } 4757 4758 /* 4759 * clear the counters or the log counters. 4760 * optname has the following values: 4761 * 0 (zero both counters and logging) 4762 * 1 (zero logging only) 4763 */ 4764 void 4765 ipfw_zero(int ac, char *av[], int optname) 4766 { 4767 ipfw_range_tlv rt; 4768 uint32_t arg; 4769 int failed = EX_OK; 4770 char const *errstr; 4771 char const *name = optname ? "RESETLOG" : "ZERO"; 4772 4773 optname = optname ? IP_FW_XRESETLOG : IP_FW_XZERO; 4774 memset(&rt, 0, sizeof(rt)); 4775 4776 av++; ac--; 4777 4778 if (ac == 0) { 4779 /* clear all entries */ 4780 rt.flags = IPFW_RCFLAG_ALL; 4781 if (do_range_cmd(optname, &rt) < 0) 4782 err(EX_UNAVAILABLE, "setsockopt(IP_FW_X%s)", name); 4783 if (!co.do_quiet) 4784 printf("%s.\n", optname == IP_FW_XZERO ? 4785 "Accounting cleared":"Logging counts reset"); 4786 4787 return; 4788 } 4789 4790 while (ac) { 4791 /* Rule number */ 4792 if (isdigit(**av)) { 4793 arg = strtonum(*av, 0, 0xffff, &errstr); 4794 if (errstr) 4795 errx(EX_DATAERR, 4796 "invalid rule number %s\n", *av); 4797 rt.start_rule = arg; 4798 rt.end_rule = arg; 4799 rt.flags |= IPFW_RCFLAG_RANGE; 4800 if (co.use_set != 0) { 4801 rt.set = co.use_set - 1; 4802 rt.flags |= IPFW_RCFLAG_SET; 4803 } 4804 if (do_range_cmd(optname, &rt) != 0) { 4805 warn("rule %u: setsockopt(IP_FW_X%s)", 4806 arg, name); 4807 failed = EX_UNAVAILABLE; 4808 } else if (rt.new_set == 0) { 4809 printf("Entry %d not found\n", arg); 4810 failed = EX_UNAVAILABLE; 4811 } else if (!co.do_quiet) 4812 printf("Entry %d %s.\n", arg, 4813 optname == IP_FW_XZERO ? 4814 "cleared" : "logging count reset"); 4815 } else { 4816 errx(EX_USAGE, "invalid rule number ``%s''", *av); 4817 } 4818 av++; ac--; 4819 } 4820 if (failed != EX_OK) 4821 exit(failed); 4822 } 4823 4824 void 4825 ipfw_flush(int force) 4826 { 4827 ipfw_range_tlv rt; 4828 4829 if (!force && !co.do_quiet) { /* need to ask user */ 4830 int c; 4831 4832 printf("Are you sure? [yn] "); 4833 fflush(stdout); 4834 do { 4835 c = toupper(getc(stdin)); 4836 while (c != '\n' && getc(stdin) != '\n') 4837 if (feof(stdin)) 4838 return; /* and do not flush */ 4839 } while (c != 'Y' && c != 'N'); 4840 printf("\n"); 4841 if (c == 'N') /* user said no */ 4842 return; 4843 } 4844 if (co.do_pipe) { 4845 dummynet_flush(); 4846 return; 4847 } 4848 /* `ipfw set N flush` - is the same that `ipfw delete set N` */ 4849 memset(&rt, 0, sizeof(rt)); 4850 if (co.use_set != 0) { 4851 rt.set = co.use_set - 1; 4852 rt.flags = IPFW_RCFLAG_SET; 4853 } else 4854 rt.flags = IPFW_RCFLAG_ALL; 4855 if (do_range_cmd(IP_FW_XDEL, &rt) != 0) 4856 err(EX_UNAVAILABLE, "setsockopt(IP_FW_XDEL)"); 4857 if (!co.do_quiet) 4858 printf("Flushed all %s.\n", co.do_pipe ? "pipes" : "rules"); 4859 } 4860 4861 static struct _s_x intcmds[] = { 4862 { "talist", TOK_TALIST }, 4863 { "iflist", TOK_IFLIST }, 4864 { "vlist", TOK_VLIST }, 4865 { NULL, 0 } 4866 }; 4867 4868 void 4869 ipfw_internal_handler(int ac, char *av[]) 4870 { 4871 int tcmd; 4872 4873 ac--; av++; 4874 NEED1("internal cmd required"); 4875 4876 if ((tcmd = match_token(intcmds, *av)) == -1) 4877 errx(EX_USAGE, "invalid internal sub-cmd: %s", *av); 4878 4879 switch (tcmd) { 4880 case TOK_IFLIST: 4881 ipfw_list_tifaces(); 4882 break; 4883 case TOK_TALIST: 4884 ipfw_list_ta(ac, av); 4885 break; 4886 case TOK_VLIST: 4887 ipfw_list_values(ac, av); 4888 break; 4889 } 4890 } 4891 4892 static int 4893 ipfw_get_tracked_ifaces(ipfw_obj_lheader **polh) 4894 { 4895 ipfw_obj_lheader req, *olh; 4896 size_t sz; 4897 4898 memset(&req, 0, sizeof(req)); 4899 sz = sizeof(req); 4900 4901 if (do_get3(IP_FW_XIFLIST, &req.opheader, &sz) != 0) { 4902 if (errno != ENOMEM) 4903 return (errno); 4904 } 4905 4906 sz = req.size; 4907 if ((olh = calloc(1, sz)) == NULL) 4908 return (ENOMEM); 4909 4910 olh->size = sz; 4911 if (do_get3(IP_FW_XIFLIST, &olh->opheader, &sz) != 0) { 4912 free(olh); 4913 return (errno); 4914 } 4915 4916 *polh = olh; 4917 return (0); 4918 } 4919 4920 static int 4921 ifinfo_cmp(const void *a, const void *b) 4922 { 4923 ipfw_iface_info *ia, *ib; 4924 4925 ia = (ipfw_iface_info *)a; 4926 ib = (ipfw_iface_info *)b; 4927 4928 return (stringnum_cmp(ia->ifname, ib->ifname)); 4929 } 4930 4931 /* 4932 * Retrieves table list from kernel, 4933 * optionally sorts it and calls requested function for each table. 4934 * Returns 0 on success. 4935 */ 4936 static void 4937 ipfw_list_tifaces() 4938 { 4939 ipfw_obj_lheader *olh; 4940 ipfw_iface_info *info; 4941 int i, error; 4942 4943 if ((error = ipfw_get_tracked_ifaces(&olh)) != 0) 4944 err(EX_OSERR, "Unable to request ipfw tracked interface list"); 4945 4946 4947 qsort(olh + 1, olh->count, olh->objsize, ifinfo_cmp); 4948 4949 info = (ipfw_iface_info *)(olh + 1); 4950 for (i = 0; i < olh->count; i++) { 4951 if (info->flags & IPFW_IFFLAG_RESOLVED) 4952 printf("%s ifindex: %d refcount: %u changes: %u\n", 4953 info->ifname, info->ifindex, info->refcnt, 4954 info->gencnt); 4955 else 4956 printf("%s ifindex: unresolved refcount: %u changes: %u\n", 4957 info->ifname, info->refcnt, info->gencnt); 4958 info = (ipfw_iface_info *)((caddr_t)info + olh->objsize); 4959 } 4960 4961 free(olh); 4962 } 4963 4964 4965 4966 4967