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, 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])) || rt.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 readsz; 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 readsz = sizeof(*cfg); 2546 rcnt = 0; 2547 2548 fo->set_mask = cfg->set_mask; 2549 2550 ctlv = (ipfw_obj_ctlv *)(cfg + 1); 2551 2552 if (cfg->flags & IPFW_CFG_GET_STATIC) { 2553 /* We've requested static rules */ 2554 if (ctlv->head.type == IPFW_TLV_TBLNAME_LIST) { 2555 fo->tstate = ctlv; 2556 readsz += ctlv->head.length; 2557 ctlv = (ipfw_obj_ctlv *)((caddr_t)ctlv + 2558 ctlv->head.length); 2559 } 2560 2561 if (ctlv->head.type == IPFW_TLV_RULE_LIST) { 2562 rbase = (ipfw_obj_tlv *)(ctlv + 1); 2563 rcnt = ctlv->count; 2564 readsz += ctlv->head.length; 2565 ctlv = (ipfw_obj_ctlv *)((caddr_t)ctlv + 2566 ctlv->head.length); 2567 } 2568 } 2569 2570 if ((cfg->flags & IPFW_CFG_GET_STATES) && (readsz != sz)) { 2571 /* We may have some dynamic states */ 2572 dynsz = sz - readsz; 2573 /* Skip empty header */ 2574 if (dynsz != sizeof(ipfw_obj_ctlv)) 2575 dynbase = (caddr_t)ctlv; 2576 else 2577 dynsz = 0; 2578 } 2579 2580 prepare_format_opts(co, fo, rbase, rcnt, dynbase, dynsz); 2581 bp_alloc(&bp, 4096); 2582 2583 /* if no rule numbers were specified, list all rules */ 2584 if (ac == 0) { 2585 fo->first = 0; 2586 fo->last = IPFW_DEFAULT_RULE; 2587 list_static_range(co, fo, &bp, rbase, rcnt); 2588 2589 if (co->do_dynamic && dynsz > 0) { 2590 printf("## Dynamic rules (%d %zu):\n", fo->dcnt, dynsz); 2591 list_dyn_range(co, fo, &bp, dynbase, dynsz); 2592 } 2593 2594 bp_free(&bp); 2595 return (EX_OK); 2596 } 2597 2598 /* display specific rules requested on command line */ 2599 for (lac = ac, lav = av; lac != 0; lac--) { 2600 /* convert command line rule # */ 2601 fo->last = fo->first = strtoul(*lav++, &endptr, 10); 2602 if (*endptr == '-') 2603 fo->last = strtoul(endptr + 1, &endptr, 10); 2604 if (*endptr) { 2605 exitval = EX_USAGE; 2606 warnx("invalid rule number: %s", *(lav - 1)); 2607 continue; 2608 } 2609 2610 if (list_static_range(co, fo, &bp, rbase, rcnt) == 0) { 2611 /* give precedence to other error(s) */ 2612 if (exitval == EX_OK) 2613 exitval = EX_UNAVAILABLE; 2614 if (fo->first == fo->last) 2615 warnx("rule %u does not exist", fo->first); 2616 else 2617 warnx("no rules in range %u-%u", 2618 fo->first, fo->last); 2619 } 2620 } 2621 2622 if (co->do_dynamic && dynsz > 0) { 2623 printf("## Dynamic rules:\n"); 2624 for (lac = ac, lav = av; lac != 0; lac--) { 2625 fo->last = fo->first = strtoul(*lav++, &endptr, 10); 2626 if (*endptr == '-') 2627 fo->last = strtoul(endptr+1, &endptr, 10); 2628 if (*endptr) 2629 /* already warned */ 2630 continue; 2631 list_dyn_range(co, fo, &bp, dynbase, dynsz); 2632 } 2633 } 2634 2635 bp_free(&bp); 2636 return (exitval); 2637 } 2638 2639 2640 /* 2641 * Retrieves current ipfw configuration of given type 2642 * and stores its pointer to @pcfg. 2643 * 2644 * Caller is responsible for freeing @pcfg. 2645 * 2646 * Returns 0 on success. 2647 */ 2648 2649 static int 2650 ipfw_get_config(struct cmdline_opts *co, struct format_opts *fo, 2651 ipfw_cfg_lheader **pcfg, size_t *psize) 2652 { 2653 ipfw_cfg_lheader *cfg; 2654 size_t sz; 2655 int i; 2656 2657 2658 if (co->test_only != 0) { 2659 fprintf(stderr, "Testing only, list disabled\n"); 2660 return (0); 2661 } 2662 2663 /* Start with some data size */ 2664 sz = 4096; 2665 cfg = NULL; 2666 2667 for (i = 0; i < 16; i++) { 2668 if (cfg != NULL) 2669 free(cfg); 2670 if ((cfg = calloc(1, sz)) == NULL) 2671 return (ENOMEM); 2672 2673 cfg->flags = fo->flags; 2674 cfg->start_rule = fo->first; 2675 cfg->end_rule = fo->last; 2676 2677 if (do_get3(IP_FW_XGET, &cfg->opheader, &sz) != 0) { 2678 if (errno != ENOMEM) { 2679 free(cfg); 2680 return (errno); 2681 } 2682 2683 /* Buffer size is not enough. Try to increase */ 2684 sz = sz * 2; 2685 if (sz < cfg->size) 2686 sz = cfg->size; 2687 continue; 2688 } 2689 2690 *pcfg = cfg; 2691 *psize = sz; 2692 return (0); 2693 } 2694 2695 free(cfg); 2696 return (ENOMEM); 2697 } 2698 2699 static int 2700 lookup_host (char *host, struct in_addr *ipaddr) 2701 { 2702 struct hostent *he; 2703 2704 if (!inet_aton(host, ipaddr)) { 2705 if ((he = gethostbyname(host)) == NULL) 2706 return(-1); 2707 *ipaddr = *(struct in_addr *)he->h_addr_list[0]; 2708 } 2709 return(0); 2710 } 2711 2712 struct tidx { 2713 ipfw_obj_ntlv *idx; 2714 uint32_t count; 2715 uint32_t size; 2716 uint16_t counter; 2717 uint8_t set; 2718 }; 2719 2720 static uint16_t 2721 pack_table(struct tidx *tstate, char *name) 2722 { 2723 int i; 2724 ipfw_obj_ntlv *ntlv; 2725 2726 if (table_check_name(name) != 0) 2727 return (0); 2728 2729 for (i = 0; i < tstate->count; i++) { 2730 if (strcmp(tstate->idx[i].name, name) != 0) 2731 continue; 2732 if (tstate->idx[i].set != tstate->set) 2733 continue; 2734 2735 return (tstate->idx[i].idx); 2736 } 2737 2738 if (tstate->count + 1 > tstate->size) { 2739 tstate->size += 4; 2740 tstate->idx = realloc(tstate->idx, tstate->size * 2741 sizeof(ipfw_obj_ntlv)); 2742 if (tstate->idx == NULL) 2743 return (0); 2744 } 2745 2746 ntlv = &tstate->idx[i]; 2747 memset(ntlv, 0, sizeof(ipfw_obj_ntlv)); 2748 strlcpy(ntlv->name, name, sizeof(ntlv->name)); 2749 ntlv->head.type = IPFW_TLV_TBL_NAME; 2750 ntlv->head.length = sizeof(ipfw_obj_ntlv); 2751 ntlv->set = tstate->set; 2752 ntlv->idx = ++tstate->counter; 2753 tstate->count++; 2754 2755 return (ntlv->idx); 2756 } 2757 2758 static void 2759 fill_table(ipfw_insn *cmd, char *av, uint8_t opcode, struct tidx *tstate) 2760 { 2761 uint32_t *d = ((ipfw_insn_u32 *)cmd)->d; 2762 uint16_t uidx; 2763 char *p; 2764 2765 if ((p = strchr(av + 6, ')')) == NULL) 2766 errx(EX_DATAERR, "forgotten parenthesis: '%s'", av); 2767 *p = '\0'; 2768 p = strchr(av + 6, ','); 2769 if (p) 2770 *p++ = '\0'; 2771 2772 if ((uidx = pack_table(tstate, av + 6)) == 0) 2773 errx(EX_DATAERR, "Invalid table name: %s", av + 6); 2774 2775 cmd->opcode = opcode; 2776 cmd->arg1 = uidx; 2777 if (p) { 2778 cmd->len |= F_INSN_SIZE(ipfw_insn_u32); 2779 d[0] = strtoul(p, NULL, 0); 2780 } else 2781 cmd->len |= F_INSN_SIZE(ipfw_insn); 2782 } 2783 2784 2785 /* 2786 * fills the addr and mask fields in the instruction as appropriate from av. 2787 * Update length as appropriate. 2788 * The following formats are allowed: 2789 * me returns O_IP_*_ME 2790 * 1.2.3.4 single IP address 2791 * 1.2.3.4:5.6.7.8 address:mask 2792 * 1.2.3.4/24 address/mask 2793 * 1.2.3.4/26{1,6,5,4,23} set of addresses in a subnet 2794 * We can have multiple comma-separated address/mask entries. 2795 */ 2796 static void 2797 fill_ip(ipfw_insn_ip *cmd, char *av, int cblen, struct tidx *tstate) 2798 { 2799 int len = 0; 2800 uint32_t *d = ((ipfw_insn_u32 *)cmd)->d; 2801 2802 cmd->o.len &= ~F_LEN_MASK; /* zero len */ 2803 2804 if (_substrcmp(av, "any") == 0) 2805 return; 2806 2807 if (_substrcmp(av, "me") == 0) { 2808 cmd->o.len |= F_INSN_SIZE(ipfw_insn); 2809 return; 2810 } 2811 2812 if (strncmp(av, "table(", 6) == 0) { 2813 fill_table(&cmd->o, av, O_IP_DST_LOOKUP, tstate); 2814 return; 2815 } 2816 2817 while (av) { 2818 /* 2819 * After the address we can have '/' or ':' indicating a mask, 2820 * ',' indicating another address follows, '{' indicating a 2821 * set of addresses of unspecified size. 2822 */ 2823 char *t = NULL, *p = strpbrk(av, "/:,{"); 2824 int masklen; 2825 char md, nd = '\0'; 2826 2827 CHECK_LENGTH(cblen, F_INSN_SIZE(ipfw_insn) + 2 + len); 2828 2829 if (p) { 2830 md = *p; 2831 *p++ = '\0'; 2832 if ((t = strpbrk(p, ",{")) != NULL) { 2833 nd = *t; 2834 *t = '\0'; 2835 } 2836 } else 2837 md = '\0'; 2838 2839 if (lookup_host(av, (struct in_addr *)&d[0]) != 0) 2840 errx(EX_NOHOST, "hostname ``%s'' unknown", av); 2841 switch (md) { 2842 case ':': 2843 if (!inet_aton(p, (struct in_addr *)&d[1])) 2844 errx(EX_DATAERR, "bad netmask ``%s''", p); 2845 break; 2846 case '/': 2847 masklen = atoi(p); 2848 if (masklen == 0) 2849 d[1] = htonl(0); /* mask */ 2850 else if (masklen > 32) 2851 errx(EX_DATAERR, "bad width ``%s''", p); 2852 else 2853 d[1] = htonl(~0 << (32 - masklen)); 2854 break; 2855 case '{': /* no mask, assume /24 and put back the '{' */ 2856 d[1] = htonl(~0 << (32 - 24)); 2857 *(--p) = md; 2858 break; 2859 2860 case ',': /* single address plus continuation */ 2861 *(--p) = md; 2862 /* FALLTHROUGH */ 2863 case 0: /* initialization value */ 2864 default: 2865 d[1] = htonl(~0); /* force /32 */ 2866 break; 2867 } 2868 d[0] &= d[1]; /* mask base address with mask */ 2869 if (t) 2870 *t = nd; 2871 /* find next separator */ 2872 if (p) 2873 p = strpbrk(p, ",{"); 2874 if (p && *p == '{') { 2875 /* 2876 * We have a set of addresses. They are stored as follows: 2877 * arg1 is the set size (powers of 2, 2..256) 2878 * addr is the base address IN HOST FORMAT 2879 * mask.. is an array of arg1 bits (rounded up to 2880 * the next multiple of 32) with bits set 2881 * for each host in the map. 2882 */ 2883 uint32_t *map = (uint32_t *)&cmd->mask; 2884 int low, high; 2885 int i = contigmask((uint8_t *)&(d[1]), 32); 2886 2887 if (len > 0) 2888 errx(EX_DATAERR, "address set cannot be in a list"); 2889 if (i < 24 || i > 31) 2890 errx(EX_DATAERR, "invalid set with mask %d\n", i); 2891 cmd->o.arg1 = 1<<(32-i); /* map length */ 2892 d[0] = ntohl(d[0]); /* base addr in host format */ 2893 cmd->o.opcode = O_IP_DST_SET; /* default */ 2894 cmd->o.len |= F_INSN_SIZE(ipfw_insn_u32) + (cmd->o.arg1+31)/32; 2895 for (i = 0; i < (cmd->o.arg1+31)/32 ; i++) 2896 map[i] = 0; /* clear map */ 2897 2898 av = p + 1; 2899 low = d[0] & 0xff; 2900 high = low + cmd->o.arg1 - 1; 2901 /* 2902 * Here, i stores the previous value when we specify a range 2903 * of addresses within a mask, e.g. 45-63. i = -1 means we 2904 * have no previous value. 2905 */ 2906 i = -1; /* previous value in a range */ 2907 while (isdigit(*av)) { 2908 char *s; 2909 int a = strtol(av, &s, 0); 2910 2911 if (s == av) { /* no parameter */ 2912 if (*av != '}') 2913 errx(EX_DATAERR, "set not closed\n"); 2914 if (i != -1) 2915 errx(EX_DATAERR, "incomplete range %d-", i); 2916 break; 2917 } 2918 if (a < low || a > high) 2919 errx(EX_DATAERR, "addr %d out of range [%d-%d]\n", 2920 a, low, high); 2921 a -= low; 2922 if (i == -1) /* no previous in range */ 2923 i = a; 2924 else { /* check that range is valid */ 2925 if (i > a) 2926 errx(EX_DATAERR, "invalid range %d-%d", 2927 i+low, a+low); 2928 if (*s == '-') 2929 errx(EX_DATAERR, "double '-' in range"); 2930 } 2931 for (; i <= a; i++) 2932 map[i/32] |= 1<<(i & 31); 2933 i = -1; 2934 if (*s == '-') 2935 i = a; 2936 else if (*s == '}') 2937 break; 2938 av = s+1; 2939 } 2940 return; 2941 } 2942 av = p; 2943 if (av) /* then *av must be a ',' */ 2944 av++; 2945 2946 /* Check this entry */ 2947 if (d[1] == 0) { /* "any", specified as x.x.x.x/0 */ 2948 /* 2949 * 'any' turns the entire list into a NOP. 2950 * 'not any' never matches, so it is removed from the 2951 * list unless it is the only item, in which case we 2952 * report an error. 2953 */ 2954 if (cmd->o.len & F_NOT) { /* "not any" never matches */ 2955 if (av == NULL && len == 0) /* only this entry */ 2956 errx(EX_DATAERR, "not any never matches"); 2957 } 2958 /* else do nothing and skip this entry */ 2959 return; 2960 } 2961 /* A single IP can be stored in an optimized format */ 2962 if (d[1] == (uint32_t)~0 && av == NULL && len == 0) { 2963 cmd->o.len |= F_INSN_SIZE(ipfw_insn_u32); 2964 return; 2965 } 2966 len += 2; /* two words... */ 2967 d += 2; 2968 } /* end while */ 2969 if (len + 1 > F_LEN_MASK) 2970 errx(EX_DATAERR, "address list too long"); 2971 cmd->o.len |= len+1; 2972 } 2973 2974 2975 /* n2mask sets n bits of the mask */ 2976 void 2977 n2mask(struct in6_addr *mask, int n) 2978 { 2979 static int minimask[9] = 2980 { 0x00, 0x80, 0xc0, 0xe0, 0xf0, 0xf8, 0xfc, 0xfe, 0xff }; 2981 u_char *p; 2982 2983 memset(mask, 0, sizeof(struct in6_addr)); 2984 p = (u_char *) mask; 2985 for (; n > 0; p++, n -= 8) { 2986 if (n >= 8) 2987 *p = 0xff; 2988 else 2989 *p = minimask[n]; 2990 } 2991 return; 2992 } 2993 2994 static void 2995 fill_flags_cmd(ipfw_insn *cmd, enum ipfw_opcodes opcode, 2996 struct _s_x *flags, char *p) 2997 { 2998 char *e; 2999 uint32_t set = 0, clear = 0; 3000 3001 if (fill_flags(flags, p, &e, &set, &clear) != 0) 3002 errx(EX_DATAERR, "invalid flag %s", e); 3003 3004 cmd->opcode = opcode; 3005 cmd->len = (cmd->len & (F_NOT | F_OR)) | 1; 3006 cmd->arg1 = (set & 0xff) | ( (clear & 0xff) << 8); 3007 } 3008 3009 3010 void 3011 ipfw_delete(char *av[]) 3012 { 3013 int i; 3014 int exitval = EX_OK; 3015 int do_set = 0; 3016 ipfw_range_tlv rt; 3017 3018 av++; 3019 NEED1("missing rule specification"); 3020 memset(&rt, 0, sizeof(rt)); 3021 if ( *av && _substrcmp(*av, "set") == 0) { 3022 /* Do not allow using the following syntax: 3023 * ipfw set N delete set M 3024 */ 3025 if (co.use_set) 3026 errx(EX_DATAERR, "invalid syntax"); 3027 do_set = 1; /* delete set */ 3028 av++; 3029 } 3030 3031 /* Rule number */ 3032 while (*av && isdigit(**av)) { 3033 i = atoi(*av); av++; 3034 if (co.do_nat) { 3035 exitval = do_cmd(IP_FW_NAT_DEL, &i, sizeof i); 3036 if (exitval) { 3037 exitval = EX_UNAVAILABLE; 3038 warn("rule %u not available", i); 3039 } 3040 } else if (co.do_pipe) { 3041 exitval = ipfw_delete_pipe(co.do_pipe, i); 3042 } else { 3043 if (do_set != 0) { 3044 rt.set = i & 31; 3045 rt.flags = IPFW_RCFLAG_SET; 3046 } else { 3047 rt.start_rule = i & 0xffff; 3048 rt.end_rule = i & 0xffff; 3049 if (rt.start_rule == 0 && rt.end_rule == 0) 3050 rt.flags |= IPFW_RCFLAG_ALL; 3051 else 3052 rt.flags |= IPFW_RCFLAG_RANGE; 3053 if (co.use_set != 0) { 3054 rt.set = co.use_set - 1; 3055 rt.flags |= IPFW_RCFLAG_SET; 3056 } 3057 } 3058 i = do_range_cmd(IP_FW_XDEL, &rt); 3059 if (i != 0) { 3060 exitval = EX_UNAVAILABLE; 3061 warn("rule %u: setsockopt(IP_FW_XDEL)", 3062 rt.start_rule); 3063 } else if (rt.new_set == 0) { 3064 exitval = EX_UNAVAILABLE; 3065 if (rt.start_rule != rt.end_rule) 3066 warnx("no rules rules in %u-%u range", 3067 rt.start_rule, rt.end_rule); 3068 else 3069 warnx("rule %u not found", 3070 rt.start_rule); 3071 } 3072 } 3073 } 3074 if (exitval != EX_OK) 3075 exit(exitval); 3076 } 3077 3078 3079 /* 3080 * fill the interface structure. We do not check the name as we can 3081 * create interfaces dynamically, so checking them at insert time 3082 * makes relatively little sense. 3083 * Interface names containing '*', '?', or '[' are assumed to be shell 3084 * patterns which match interfaces. 3085 */ 3086 static void 3087 fill_iface(ipfw_insn_if *cmd, char *arg, int cblen, struct tidx *tstate) 3088 { 3089 char *p; 3090 uint16_t uidx; 3091 3092 cmd->name[0] = '\0'; 3093 cmd->o.len |= F_INSN_SIZE(ipfw_insn_if); 3094 3095 CHECK_CMDLEN; 3096 3097 /* Parse the interface or address */ 3098 if (strcmp(arg, "any") == 0) 3099 cmd->o.len = 0; /* effectively ignore this command */ 3100 else if (strncmp(arg, "table(", 6) == 0) { 3101 if ((p = strchr(arg + 6, ')')) == NULL) 3102 errx(EX_DATAERR, "forgotten parenthesis: '%s'", arg); 3103 *p = '\0'; 3104 p = strchr(arg + 6, ','); 3105 if (p) 3106 *p++ = '\0'; 3107 if ((uidx = pack_table(tstate, arg + 6)) == 0) 3108 errx(EX_DATAERR, "Invalid table name: %s", arg + 6); 3109 3110 cmd->name[0] = '\1'; /* Special value indicating table */ 3111 cmd->p.kidx = uidx; 3112 } else if (!isdigit(*arg)) { 3113 strlcpy(cmd->name, arg, sizeof(cmd->name)); 3114 cmd->p.glob = strpbrk(arg, "*?[") != NULL ? 1 : 0; 3115 } else if (!inet_aton(arg, &cmd->p.ip)) 3116 errx(EX_DATAERR, "bad ip address ``%s''", arg); 3117 } 3118 3119 static void 3120 get_mac_addr_mask(const char *p, uint8_t *addr, uint8_t *mask) 3121 { 3122 int i; 3123 size_t l; 3124 char *ap, *ptr, *optr; 3125 struct ether_addr *mac; 3126 const char *macset = "0123456789abcdefABCDEF:"; 3127 3128 if (strcmp(p, "any") == 0) { 3129 for (i = 0; i < ETHER_ADDR_LEN; i++) 3130 addr[i] = mask[i] = 0; 3131 return; 3132 } 3133 3134 optr = ptr = strdup(p); 3135 if ((ap = strsep(&ptr, "&/")) != NULL && *ap != 0) { 3136 l = strlen(ap); 3137 if (strspn(ap, macset) != l || (mac = ether_aton(ap)) == NULL) 3138 errx(EX_DATAERR, "Incorrect MAC address"); 3139 bcopy(mac, addr, ETHER_ADDR_LEN); 3140 } else 3141 errx(EX_DATAERR, "Incorrect MAC address"); 3142 3143 if (ptr != NULL) { /* we have mask? */ 3144 if (p[ptr - optr - 1] == '/') { /* mask len */ 3145 long ml = strtol(ptr, &ap, 10); 3146 if (*ap != 0 || ml > ETHER_ADDR_LEN * 8 || ml < 0) 3147 errx(EX_DATAERR, "Incorrect mask length"); 3148 for (i = 0; ml > 0 && i < ETHER_ADDR_LEN; ml -= 8, i++) 3149 mask[i] = (ml >= 8) ? 0xff: (~0) << (8 - ml); 3150 } else { /* mask */ 3151 l = strlen(ptr); 3152 if (strspn(ptr, macset) != l || 3153 (mac = ether_aton(ptr)) == NULL) 3154 errx(EX_DATAERR, "Incorrect mask"); 3155 bcopy(mac, mask, ETHER_ADDR_LEN); 3156 } 3157 } else { /* default mask: ff:ff:ff:ff:ff:ff */ 3158 for (i = 0; i < ETHER_ADDR_LEN; i++) 3159 mask[i] = 0xff; 3160 } 3161 for (i = 0; i < ETHER_ADDR_LEN; i++) 3162 addr[i] &= mask[i]; 3163 3164 free(optr); 3165 } 3166 3167 /* 3168 * helper function, updates the pointer to cmd with the length 3169 * of the current command, and also cleans up the first word of 3170 * the new command in case it has been clobbered before. 3171 */ 3172 static ipfw_insn * 3173 next_cmd(ipfw_insn *cmd, int *len) 3174 { 3175 *len -= F_LEN(cmd); 3176 CHECK_LENGTH(*len, 0); 3177 cmd += F_LEN(cmd); 3178 bzero(cmd, sizeof(*cmd)); 3179 return cmd; 3180 } 3181 3182 /* 3183 * Takes arguments and copies them into a comment 3184 */ 3185 static void 3186 fill_comment(ipfw_insn *cmd, char **av, int cblen) 3187 { 3188 int i, l; 3189 char *p = (char *)(cmd + 1); 3190 3191 cmd->opcode = O_NOP; 3192 cmd->len = (cmd->len & (F_NOT | F_OR)); 3193 3194 /* Compute length of comment string. */ 3195 for (i = 0, l = 0; av[i] != NULL; i++) 3196 l += strlen(av[i]) + 1; 3197 if (l == 0) 3198 return; 3199 if (l > 84) 3200 errx(EX_DATAERR, 3201 "comment too long (max 80 chars)"); 3202 l = 1 + (l+3)/4; 3203 cmd->len = (cmd->len & (F_NOT | F_OR)) | l; 3204 CHECK_CMDLEN; 3205 3206 for (i = 0; av[i] != NULL; i++) { 3207 strcpy(p, av[i]); 3208 p += strlen(av[i]); 3209 *p++ = ' '; 3210 } 3211 *(--p) = '\0'; 3212 } 3213 3214 /* 3215 * A function to fill simple commands of size 1. 3216 * Existing flags are preserved. 3217 */ 3218 static void 3219 fill_cmd(ipfw_insn *cmd, enum ipfw_opcodes opcode, int flags, uint16_t arg) 3220 { 3221 cmd->opcode = opcode; 3222 cmd->len = ((cmd->len | flags) & (F_NOT | F_OR)) | 1; 3223 cmd->arg1 = arg; 3224 } 3225 3226 /* 3227 * Fetch and add the MAC address and type, with masks. This generates one or 3228 * two microinstructions, and returns the pointer to the last one. 3229 */ 3230 static ipfw_insn * 3231 add_mac(ipfw_insn *cmd, char *av[], int cblen) 3232 { 3233 ipfw_insn_mac *mac; 3234 3235 if ( ( av[0] == NULL ) || ( av[1] == NULL ) ) 3236 errx(EX_DATAERR, "MAC dst src"); 3237 3238 cmd->opcode = O_MACADDR2; 3239 cmd->len = (cmd->len & (F_NOT | F_OR)) | F_INSN_SIZE(ipfw_insn_mac); 3240 CHECK_CMDLEN; 3241 3242 mac = (ipfw_insn_mac *)cmd; 3243 get_mac_addr_mask(av[0], mac->addr, mac->mask); /* dst */ 3244 get_mac_addr_mask(av[1], &(mac->addr[ETHER_ADDR_LEN]), 3245 &(mac->mask[ETHER_ADDR_LEN])); /* src */ 3246 return cmd; 3247 } 3248 3249 static ipfw_insn * 3250 add_mactype(ipfw_insn *cmd, char *av, int cblen) 3251 { 3252 if (!av) 3253 errx(EX_DATAERR, "missing MAC type"); 3254 if (strcmp(av, "any") != 0) { /* we have a non-null type */ 3255 fill_newports((ipfw_insn_u16 *)cmd, av, IPPROTO_ETHERTYPE, 3256 cblen); 3257 cmd->opcode = O_MAC_TYPE; 3258 return cmd; 3259 } else 3260 return NULL; 3261 } 3262 3263 static ipfw_insn * 3264 add_proto0(ipfw_insn *cmd, char *av, u_char *protop) 3265 { 3266 struct protoent *pe; 3267 char *ep; 3268 int proto; 3269 3270 proto = strtol(av, &ep, 10); 3271 if (*ep != '\0' || proto <= 0) { 3272 if ((pe = getprotobyname(av)) == NULL) 3273 return NULL; 3274 proto = pe->p_proto; 3275 } 3276 3277 fill_cmd(cmd, O_PROTO, 0, proto); 3278 *protop = proto; 3279 return cmd; 3280 } 3281 3282 static ipfw_insn * 3283 add_proto(ipfw_insn *cmd, char *av, u_char *protop) 3284 { 3285 u_char proto = IPPROTO_IP; 3286 3287 if (_substrcmp(av, "all") == 0 || strcmp(av, "ip") == 0) 3288 ; /* do not set O_IP4 nor O_IP6 */ 3289 else if (strcmp(av, "ip4") == 0) 3290 /* explicit "just IPv4" rule */ 3291 fill_cmd(cmd, O_IP4, 0, 0); 3292 else if (strcmp(av, "ip6") == 0) { 3293 /* explicit "just IPv6" rule */ 3294 proto = IPPROTO_IPV6; 3295 fill_cmd(cmd, O_IP6, 0, 0); 3296 } else 3297 return add_proto0(cmd, av, protop); 3298 3299 *protop = proto; 3300 return cmd; 3301 } 3302 3303 static ipfw_insn * 3304 add_proto_compat(ipfw_insn *cmd, char *av, u_char *protop) 3305 { 3306 u_char proto = IPPROTO_IP; 3307 3308 if (_substrcmp(av, "all") == 0 || strcmp(av, "ip") == 0) 3309 ; /* do not set O_IP4 nor O_IP6 */ 3310 else if (strcmp(av, "ipv4") == 0 || strcmp(av, "ip4") == 0) 3311 /* explicit "just IPv4" rule */ 3312 fill_cmd(cmd, O_IP4, 0, 0); 3313 else if (strcmp(av, "ipv6") == 0 || strcmp(av, "ip6") == 0) { 3314 /* explicit "just IPv6" rule */ 3315 proto = IPPROTO_IPV6; 3316 fill_cmd(cmd, O_IP6, 0, 0); 3317 } else 3318 return add_proto0(cmd, av, protop); 3319 3320 *protop = proto; 3321 return cmd; 3322 } 3323 3324 static ipfw_insn * 3325 add_srcip(ipfw_insn *cmd, char *av, int cblen, struct tidx *tstate) 3326 { 3327 fill_ip((ipfw_insn_ip *)cmd, av, cblen, tstate); 3328 if (cmd->opcode == O_IP_DST_SET) /* set */ 3329 cmd->opcode = O_IP_SRC_SET; 3330 else if (cmd->opcode == O_IP_DST_LOOKUP) /* table */ 3331 cmd->opcode = O_IP_SRC_LOOKUP; 3332 else if (F_LEN(cmd) == F_INSN_SIZE(ipfw_insn)) /* me */ 3333 cmd->opcode = O_IP_SRC_ME; 3334 else if (F_LEN(cmd) == F_INSN_SIZE(ipfw_insn_u32)) /* one IP */ 3335 cmd->opcode = O_IP_SRC; 3336 else /* addr/mask */ 3337 cmd->opcode = O_IP_SRC_MASK; 3338 return cmd; 3339 } 3340 3341 static ipfw_insn * 3342 add_dstip(ipfw_insn *cmd, char *av, int cblen, struct tidx *tstate) 3343 { 3344 fill_ip((ipfw_insn_ip *)cmd, av, cblen, tstate); 3345 if (cmd->opcode == O_IP_DST_SET) /* set */ 3346 ; 3347 else if (cmd->opcode == O_IP_DST_LOOKUP) /* table */ 3348 ; 3349 else if (F_LEN(cmd) == F_INSN_SIZE(ipfw_insn)) /* me */ 3350 cmd->opcode = O_IP_DST_ME; 3351 else if (F_LEN(cmd) == F_INSN_SIZE(ipfw_insn_u32)) /* one IP */ 3352 cmd->opcode = O_IP_DST; 3353 else /* addr/mask */ 3354 cmd->opcode = O_IP_DST_MASK; 3355 return cmd; 3356 } 3357 3358 static struct _s_x f_reserved_keywords[] = { 3359 { "altq", TOK_OR }, 3360 { "//", TOK_OR }, 3361 { "diverted", TOK_OR }, 3362 { "dst-port", TOK_OR }, 3363 { "src-port", TOK_OR }, 3364 { "established", TOK_OR }, 3365 { "keep-state", TOK_OR }, 3366 { "frag", TOK_OR }, 3367 { "icmptypes", TOK_OR }, 3368 { "in", TOK_OR }, 3369 { "out", TOK_OR }, 3370 { "ip6", TOK_OR }, 3371 { "any", TOK_OR }, 3372 { "to", TOK_OR }, 3373 { "via", TOK_OR }, 3374 { "{", TOK_OR }, 3375 { NULL, 0 } /* terminator */ 3376 }; 3377 3378 static ipfw_insn * 3379 add_ports(ipfw_insn *cmd, char *av, u_char proto, int opcode, int cblen) 3380 { 3381 3382 if (match_token(f_reserved_keywords, av) != -1) 3383 return (NULL); 3384 3385 if (fill_newports((ipfw_insn_u16 *)cmd, av, proto, cblen)) { 3386 /* XXX todo: check that we have a protocol with ports */ 3387 cmd->opcode = opcode; 3388 return cmd; 3389 } 3390 return NULL; 3391 } 3392 3393 static ipfw_insn * 3394 add_src(ipfw_insn *cmd, char *av, u_char proto, int cblen, struct tidx *tstate) 3395 { 3396 struct in6_addr a; 3397 char *host, *ch, buf[INET6_ADDRSTRLEN]; 3398 ipfw_insn *ret = NULL; 3399 int len; 3400 3401 /* Copy first address in set if needed */ 3402 if ((ch = strpbrk(av, "/,")) != NULL) { 3403 len = ch - av; 3404 strlcpy(buf, av, sizeof(buf)); 3405 if (len < sizeof(buf)) 3406 buf[len] = '\0'; 3407 host = buf; 3408 } else 3409 host = av; 3410 3411 if (proto == IPPROTO_IPV6 || strcmp(av, "me6") == 0 || 3412 inet_pton(AF_INET6, host, &a) == 1) 3413 ret = add_srcip6(cmd, av, cblen); 3414 /* XXX: should check for IPv4, not !IPv6 */ 3415 if (ret == NULL && (proto == IPPROTO_IP || strcmp(av, "me") == 0 || 3416 inet_pton(AF_INET6, host, &a) != 1)) 3417 ret = add_srcip(cmd, av, cblen, tstate); 3418 if (ret == NULL && strcmp(av, "any") != 0) 3419 ret = cmd; 3420 3421 return ret; 3422 } 3423 3424 static ipfw_insn * 3425 add_dst(ipfw_insn *cmd, char *av, u_char proto, int cblen, struct tidx *tstate) 3426 { 3427 struct in6_addr a; 3428 char *host, *ch, buf[INET6_ADDRSTRLEN]; 3429 ipfw_insn *ret = NULL; 3430 int len; 3431 3432 /* Copy first address in set if needed */ 3433 if ((ch = strpbrk(av, "/,")) != NULL) { 3434 len = ch - av; 3435 strlcpy(buf, av, sizeof(buf)); 3436 if (len < sizeof(buf)) 3437 buf[len] = '\0'; 3438 host = buf; 3439 } else 3440 host = av; 3441 3442 if (proto == IPPROTO_IPV6 || strcmp(av, "me6") == 0 || 3443 inet_pton(AF_INET6, host, &a) == 1) 3444 ret = add_dstip6(cmd, av, cblen); 3445 /* XXX: should check for IPv4, not !IPv6 */ 3446 if (ret == NULL && (proto == IPPROTO_IP || strcmp(av, "me") == 0 || 3447 inet_pton(AF_INET6, host, &a) != 1)) 3448 ret = add_dstip(cmd, av, cblen, tstate); 3449 if (ret == NULL && strcmp(av, "any") != 0) 3450 ret = cmd; 3451 3452 return ret; 3453 } 3454 3455 /* 3456 * Parse arguments and assemble the microinstructions which make up a rule. 3457 * Rules are added into the 'rulebuf' and then copied in the correct order 3458 * into the actual rule. 3459 * 3460 * The syntax for a rule starts with the action, followed by 3461 * optional action parameters, and the various match patterns. 3462 * In the assembled microcode, the first opcode must be an O_PROBE_STATE 3463 * (generated if the rule includes a keep-state option), then the 3464 * various match patterns, log/altq actions, and the actual action. 3465 * 3466 */ 3467 void 3468 compile_rule(char *av[], uint32_t *rbuf, int *rbufsize, struct tidx *tstate) 3469 { 3470 /* 3471 * rules are added into the 'rulebuf' and then copied in 3472 * the correct order into the actual rule. 3473 * Some things that need to go out of order (prob, action etc.) 3474 * go into actbuf[]. 3475 */ 3476 static uint32_t actbuf[255], cmdbuf[255]; 3477 int rblen, ablen, cblen; 3478 3479 ipfw_insn *src, *dst, *cmd, *action, *prev=NULL; 3480 ipfw_insn *first_cmd; /* first match pattern */ 3481 3482 struct ip_fw_rule *rule; 3483 3484 /* 3485 * various flags used to record that we entered some fields. 3486 */ 3487 ipfw_insn *have_state = NULL; /* check-state or keep-state */ 3488 ipfw_insn *have_log = NULL, *have_altq = NULL, *have_tag = NULL; 3489 size_t len; 3490 3491 int i; 3492 3493 int open_par = 0; /* open parenthesis ( */ 3494 3495 /* proto is here because it is used to fetch ports */ 3496 u_char proto = IPPROTO_IP; /* default protocol */ 3497 3498 double match_prob = 1; /* match probability, default is always match */ 3499 3500 bzero(actbuf, sizeof(actbuf)); /* actions go here */ 3501 bzero(cmdbuf, sizeof(cmdbuf)); 3502 bzero(rbuf, *rbufsize); 3503 3504 rule = (struct ip_fw_rule *)rbuf; 3505 cmd = (ipfw_insn *)cmdbuf; 3506 action = (ipfw_insn *)actbuf; 3507 3508 rblen = *rbufsize / sizeof(uint32_t); 3509 rblen -= sizeof(struct ip_fw_rule) / sizeof(uint32_t); 3510 ablen = sizeof(actbuf) / sizeof(actbuf[0]); 3511 cblen = sizeof(cmdbuf) / sizeof(cmdbuf[0]); 3512 cblen -= F_INSN_SIZE(ipfw_insn_u32) + 1; 3513 3514 #define CHECK_RBUFLEN(len) { CHECK_LENGTH(rblen, len); rblen -= len; } 3515 #define CHECK_ACTLEN CHECK_LENGTH(ablen, action->len) 3516 3517 av++; 3518 3519 /* [rule N] -- Rule number optional */ 3520 if (av[0] && isdigit(**av)) { 3521 rule->rulenum = atoi(*av); 3522 av++; 3523 } 3524 3525 /* [set N] -- set number (0..RESVD_SET), optional */ 3526 if (av[0] && av[1] && _substrcmp(*av, "set") == 0) { 3527 int set = strtoul(av[1], NULL, 10); 3528 if (set < 0 || set > RESVD_SET) 3529 errx(EX_DATAERR, "illegal set %s", av[1]); 3530 rule->set = set; 3531 tstate->set = set; 3532 av += 2; 3533 } 3534 3535 /* [prob D] -- match probability, optional */ 3536 if (av[0] && av[1] && _substrcmp(*av, "prob") == 0) { 3537 match_prob = strtod(av[1], NULL); 3538 3539 if (match_prob <= 0 || match_prob > 1) 3540 errx(EX_DATAERR, "illegal match prob. %s", av[1]); 3541 av += 2; 3542 } 3543 3544 /* action -- mandatory */ 3545 NEED1("missing action"); 3546 i = match_token(rule_actions, *av); 3547 av++; 3548 action->len = 1; /* default */ 3549 CHECK_ACTLEN; 3550 switch(i) { 3551 case TOK_CHECKSTATE: 3552 have_state = action; 3553 action->opcode = O_CHECK_STATE; 3554 break; 3555 3556 case TOK_ACCEPT: 3557 action->opcode = O_ACCEPT; 3558 break; 3559 3560 case TOK_DENY: 3561 action->opcode = O_DENY; 3562 action->arg1 = 0; 3563 break; 3564 3565 case TOK_REJECT: 3566 action->opcode = O_REJECT; 3567 action->arg1 = ICMP_UNREACH_HOST; 3568 break; 3569 3570 case TOK_RESET: 3571 action->opcode = O_REJECT; 3572 action->arg1 = ICMP_REJECT_RST; 3573 break; 3574 3575 case TOK_RESET6: 3576 action->opcode = O_UNREACH6; 3577 action->arg1 = ICMP6_UNREACH_RST; 3578 break; 3579 3580 case TOK_UNREACH: 3581 action->opcode = O_REJECT; 3582 NEED1("missing reject code"); 3583 fill_reject_code(&action->arg1, *av); 3584 av++; 3585 break; 3586 3587 case TOK_UNREACH6: 3588 action->opcode = O_UNREACH6; 3589 NEED1("missing unreach code"); 3590 fill_unreach6_code(&action->arg1, *av); 3591 av++; 3592 break; 3593 3594 case TOK_COUNT: 3595 action->opcode = O_COUNT; 3596 break; 3597 3598 case TOK_NAT: 3599 action->opcode = O_NAT; 3600 action->len = F_INSN_SIZE(ipfw_insn_nat); 3601 CHECK_ACTLEN; 3602 if (_substrcmp(*av, "global") == 0) { 3603 action->arg1 = 0; 3604 av++; 3605 break; 3606 } else 3607 goto chkarg; 3608 3609 case TOK_QUEUE: 3610 action->opcode = O_QUEUE; 3611 goto chkarg; 3612 case TOK_PIPE: 3613 action->opcode = O_PIPE; 3614 goto chkarg; 3615 case TOK_SKIPTO: 3616 action->opcode = O_SKIPTO; 3617 goto chkarg; 3618 case TOK_NETGRAPH: 3619 action->opcode = O_NETGRAPH; 3620 goto chkarg; 3621 case TOK_NGTEE: 3622 action->opcode = O_NGTEE; 3623 goto chkarg; 3624 case TOK_DIVERT: 3625 action->opcode = O_DIVERT; 3626 goto chkarg; 3627 case TOK_TEE: 3628 action->opcode = O_TEE; 3629 goto chkarg; 3630 case TOK_CALL: 3631 action->opcode = O_CALLRETURN; 3632 chkarg: 3633 if (!av[0]) 3634 errx(EX_USAGE, "missing argument for %s", *(av - 1)); 3635 if (isdigit(**av)) { 3636 action->arg1 = strtoul(*av, NULL, 10); 3637 if (action->arg1 <= 0 || action->arg1 >= IP_FW_TABLEARG) 3638 errx(EX_DATAERR, "illegal argument for %s", 3639 *(av - 1)); 3640 } else if (_substrcmp(*av, "tablearg") == 0) { 3641 action->arg1 = IP_FW_TARG; 3642 } else if (i == TOK_DIVERT || i == TOK_TEE) { 3643 struct servent *s; 3644 setservent(1); 3645 s = getservbyname(av[0], "divert"); 3646 if (s != NULL) 3647 action->arg1 = ntohs(s->s_port); 3648 else 3649 errx(EX_DATAERR, "illegal divert/tee port"); 3650 } else 3651 errx(EX_DATAERR, "illegal argument for %s", *(av - 1)); 3652 av++; 3653 break; 3654 3655 case TOK_FORWARD: { 3656 /* 3657 * Locate the address-port separator (':' or ','). 3658 * Could be one of the following: 3659 * hostname:port 3660 * IPv4 a.b.c.d,port 3661 * IPv4 a.b.c.d:port 3662 * IPv6 w:x:y::z,port 3663 * The ':' can only be used with hostname and IPv4 address. 3664 * XXX-BZ Should we also support [w:x:y::z]:port? 3665 */ 3666 struct sockaddr_storage result; 3667 struct addrinfo *res; 3668 char *s, *end; 3669 int family; 3670 u_short port_number; 3671 3672 NEED1("missing forward address[:port]"); 3673 3674 /* 3675 * locate the address-port separator (':' or ',') 3676 */ 3677 s = strchr(*av, ','); 3678 if (s == NULL) { 3679 /* Distinguish between IPv4:port and IPv6 cases. */ 3680 s = strchr(*av, ':'); 3681 if (s && strchr(s+1, ':')) 3682 s = NULL; /* no port */ 3683 } 3684 3685 port_number = 0; 3686 if (s != NULL) { 3687 /* Terminate host portion and set s to start of port. */ 3688 *(s++) = '\0'; 3689 i = strtoport(s, &end, 0 /* base */, 0 /* proto */); 3690 if (s == end) 3691 errx(EX_DATAERR, 3692 "illegal forwarding port ``%s''", s); 3693 port_number = (u_short)i; 3694 } 3695 3696 if (_substrcmp(*av, "tablearg") == 0) { 3697 family = PF_INET; 3698 ((struct sockaddr_in*)&result)->sin_addr.s_addr = 3699 INADDR_ANY; 3700 } else { 3701 /* 3702 * Resolve the host name or address to a family and a 3703 * network representation of the address. 3704 */ 3705 if (getaddrinfo(*av, NULL, NULL, &res)) 3706 errx(EX_DATAERR, NULL); 3707 /* Just use the first host in the answer. */ 3708 family = res->ai_family; 3709 memcpy(&result, res->ai_addr, res->ai_addrlen); 3710 freeaddrinfo(res); 3711 } 3712 3713 if (family == PF_INET) { 3714 ipfw_insn_sa *p = (ipfw_insn_sa *)action; 3715 3716 action->opcode = O_FORWARD_IP; 3717 action->len = F_INSN_SIZE(ipfw_insn_sa); 3718 CHECK_ACTLEN; 3719 3720 /* 3721 * In the kernel we assume AF_INET and use only 3722 * sin_port and sin_addr. Remember to set sin_len as 3723 * the routing code seems to use it too. 3724 */ 3725 p->sa.sin_len = sizeof(struct sockaddr_in); 3726 p->sa.sin_family = AF_INET; 3727 p->sa.sin_port = port_number; 3728 p->sa.sin_addr.s_addr = 3729 ((struct sockaddr_in *)&result)->sin_addr.s_addr; 3730 } else if (family == PF_INET6) { 3731 ipfw_insn_sa6 *p = (ipfw_insn_sa6 *)action; 3732 3733 action->opcode = O_FORWARD_IP6; 3734 action->len = F_INSN_SIZE(ipfw_insn_sa6); 3735 CHECK_ACTLEN; 3736 3737 p->sa.sin6_len = sizeof(struct sockaddr_in6); 3738 p->sa.sin6_family = AF_INET6; 3739 p->sa.sin6_port = port_number; 3740 p->sa.sin6_flowinfo = 0; 3741 p->sa.sin6_scope_id = 0; 3742 /* No table support for v6 yet. */ 3743 bcopy(&((struct sockaddr_in6*)&result)->sin6_addr, 3744 &p->sa.sin6_addr, sizeof(p->sa.sin6_addr)); 3745 } else { 3746 errx(EX_DATAERR, "Invalid address family in forward action"); 3747 } 3748 av++; 3749 break; 3750 } 3751 case TOK_COMMENT: 3752 /* pretend it is a 'count' rule followed by the comment */ 3753 action->opcode = O_COUNT; 3754 av--; /* go back... */ 3755 break; 3756 3757 case TOK_SETFIB: 3758 { 3759 int numfibs; 3760 size_t intsize = sizeof(int); 3761 3762 action->opcode = O_SETFIB; 3763 NEED1("missing fib number"); 3764 if (_substrcmp(*av, "tablearg") == 0) { 3765 action->arg1 = IP_FW_TARG; 3766 } else { 3767 action->arg1 = strtoul(*av, NULL, 10); 3768 if (sysctlbyname("net.fibs", &numfibs, &intsize, 3769 NULL, 0) == -1) 3770 errx(EX_DATAERR, "fibs not suported.\n"); 3771 if (action->arg1 >= numfibs) /* Temporary */ 3772 errx(EX_DATAERR, "fib too large.\n"); 3773 /* Add high-order bit to fib to make room for tablearg*/ 3774 action->arg1 |= 0x8000; 3775 } 3776 av++; 3777 break; 3778 } 3779 3780 case TOK_SETDSCP: 3781 { 3782 int code; 3783 3784 action->opcode = O_SETDSCP; 3785 NEED1("missing DSCP code"); 3786 if (_substrcmp(*av, "tablearg") == 0) { 3787 action->arg1 = IP_FW_TARG; 3788 } else if (isalpha(*av[0])) { 3789 if ((code = match_token(f_ipdscp, *av)) == -1) 3790 errx(EX_DATAERR, "Unknown DSCP code"); 3791 action->arg1 = code; 3792 } else 3793 action->arg1 = strtoul(*av, NULL, 10); 3794 /* Add high-order bit to DSCP to make room for tablearg */ 3795 if (action->arg1 != IP_FW_TARG) 3796 action->arg1 |= 0x8000; 3797 av++; 3798 break; 3799 } 3800 3801 case TOK_REASS: 3802 action->opcode = O_REASS; 3803 break; 3804 3805 case TOK_RETURN: 3806 fill_cmd(action, O_CALLRETURN, F_NOT, 0); 3807 break; 3808 3809 default: 3810 errx(EX_DATAERR, "invalid action %s\n", av[-1]); 3811 } 3812 action = next_cmd(action, &ablen); 3813 3814 /* 3815 * [altq queuename] -- altq tag, optional 3816 * [log [logamount N]] -- log, optional 3817 * 3818 * If they exist, it go first in the cmdbuf, but then it is 3819 * skipped in the copy section to the end of the buffer. 3820 */ 3821 while (av[0] != NULL && (i = match_token(rule_action_params, *av)) != -1) { 3822 av++; 3823 switch (i) { 3824 case TOK_LOG: 3825 { 3826 ipfw_insn_log *c = (ipfw_insn_log *)cmd; 3827 int l; 3828 3829 if (have_log) 3830 errx(EX_DATAERR, 3831 "log cannot be specified more than once"); 3832 have_log = (ipfw_insn *)c; 3833 cmd->len = F_INSN_SIZE(ipfw_insn_log); 3834 CHECK_CMDLEN; 3835 cmd->opcode = O_LOG; 3836 if (av[0] && _substrcmp(*av, "logamount") == 0) { 3837 av++; 3838 NEED1("logamount requires argument"); 3839 l = atoi(*av); 3840 if (l < 0) 3841 errx(EX_DATAERR, 3842 "logamount must be positive"); 3843 c->max_log = l; 3844 av++; 3845 } else { 3846 len = sizeof(c->max_log); 3847 if (sysctlbyname("net.inet.ip.fw.verbose_limit", 3848 &c->max_log, &len, NULL, 0) == -1) { 3849 if (co.test_only) { 3850 c->max_log = 0; 3851 break; 3852 } 3853 errx(1, "sysctlbyname(\"%s\")", 3854 "net.inet.ip.fw.verbose_limit"); 3855 } 3856 } 3857 } 3858 break; 3859 3860 #ifndef NO_ALTQ 3861 case TOK_ALTQ: 3862 { 3863 ipfw_insn_altq *a = (ipfw_insn_altq *)cmd; 3864 3865 NEED1("missing altq queue name"); 3866 if (have_altq) 3867 errx(EX_DATAERR, 3868 "altq cannot be specified more than once"); 3869 have_altq = (ipfw_insn *)a; 3870 cmd->len = F_INSN_SIZE(ipfw_insn_altq); 3871 CHECK_CMDLEN; 3872 cmd->opcode = O_ALTQ; 3873 a->qid = altq_name_to_qid(*av); 3874 av++; 3875 } 3876 break; 3877 #endif 3878 3879 case TOK_TAG: 3880 case TOK_UNTAG: { 3881 uint16_t tag; 3882 3883 if (have_tag) 3884 errx(EX_USAGE, "tag and untag cannot be " 3885 "specified more than once"); 3886 GET_UINT_ARG(tag, IPFW_ARG_MIN, IPFW_ARG_MAX, i, 3887 rule_action_params); 3888 have_tag = cmd; 3889 fill_cmd(cmd, O_TAG, (i == TOK_TAG) ? 0: F_NOT, tag); 3890 av++; 3891 break; 3892 } 3893 3894 default: 3895 abort(); 3896 } 3897 cmd = next_cmd(cmd, &cblen); 3898 } 3899 3900 if (have_state) /* must be a check-state, we are done */ 3901 goto done; 3902 3903 #define OR_START(target) \ 3904 if (av[0] && (*av[0] == '(' || *av[0] == '{')) { \ 3905 if (open_par) \ 3906 errx(EX_USAGE, "nested \"(\" not allowed\n"); \ 3907 prev = NULL; \ 3908 open_par = 1; \ 3909 if ( (av[0])[1] == '\0') { \ 3910 av++; \ 3911 } else \ 3912 (*av)++; \ 3913 } \ 3914 target: \ 3915 3916 3917 #define CLOSE_PAR \ 3918 if (open_par) { \ 3919 if (av[0] && ( \ 3920 strcmp(*av, ")") == 0 || \ 3921 strcmp(*av, "}") == 0)) { \ 3922 prev = NULL; \ 3923 open_par = 0; \ 3924 av++; \ 3925 } else \ 3926 errx(EX_USAGE, "missing \")\"\n"); \ 3927 } 3928 3929 #define NOT_BLOCK \ 3930 if (av[0] && _substrcmp(*av, "not") == 0) { \ 3931 if (cmd->len & F_NOT) \ 3932 errx(EX_USAGE, "double \"not\" not allowed\n"); \ 3933 cmd->len |= F_NOT; \ 3934 av++; \ 3935 } 3936 3937 #define OR_BLOCK(target) \ 3938 if (av[0] && _substrcmp(*av, "or") == 0) { \ 3939 if (prev == NULL || open_par == 0) \ 3940 errx(EX_DATAERR, "invalid OR block"); \ 3941 prev->len |= F_OR; \ 3942 av++; \ 3943 goto target; \ 3944 } \ 3945 CLOSE_PAR; 3946 3947 first_cmd = cmd; 3948 3949 #if 0 3950 /* 3951 * MAC addresses, optional. 3952 * If we have this, we skip the part "proto from src to dst" 3953 * and jump straight to the option parsing. 3954 */ 3955 NOT_BLOCK; 3956 NEED1("missing protocol"); 3957 if (_substrcmp(*av, "MAC") == 0 || 3958 _substrcmp(*av, "mac") == 0) { 3959 av++; /* the "MAC" keyword */ 3960 add_mac(cmd, av); /* exits in case of errors */ 3961 cmd = next_cmd(cmd); 3962 av += 2; /* dst-mac and src-mac */ 3963 NOT_BLOCK; 3964 NEED1("missing mac type"); 3965 if (add_mactype(cmd, av[0])) 3966 cmd = next_cmd(cmd); 3967 av++; /* any or mac-type */ 3968 goto read_options; 3969 } 3970 #endif 3971 3972 /* 3973 * protocol, mandatory 3974 */ 3975 OR_START(get_proto); 3976 NOT_BLOCK; 3977 NEED1("missing protocol"); 3978 if (add_proto_compat(cmd, *av, &proto)) { 3979 av++; 3980 if (F_LEN(cmd) != 0) { 3981 prev = cmd; 3982 cmd = next_cmd(cmd, &cblen); 3983 } 3984 } else if (first_cmd != cmd) { 3985 errx(EX_DATAERR, "invalid protocol ``%s''", *av); 3986 } else 3987 goto read_options; 3988 OR_BLOCK(get_proto); 3989 3990 /* 3991 * "from", mandatory 3992 */ 3993 if ((av[0] == NULL) || _substrcmp(*av, "from") != 0) 3994 errx(EX_USAGE, "missing ``from''"); 3995 av++; 3996 3997 /* 3998 * source IP, mandatory 3999 */ 4000 OR_START(source_ip); 4001 NOT_BLOCK; /* optional "not" */ 4002 NEED1("missing source address"); 4003 if (add_src(cmd, *av, proto, cblen, tstate)) { 4004 av++; 4005 if (F_LEN(cmd) != 0) { /* ! any */ 4006 prev = cmd; 4007 cmd = next_cmd(cmd, &cblen); 4008 } 4009 } else 4010 errx(EX_USAGE, "bad source address %s", *av); 4011 OR_BLOCK(source_ip); 4012 4013 /* 4014 * source ports, optional 4015 */ 4016 NOT_BLOCK; /* optional "not" */ 4017 if ( av[0] != NULL ) { 4018 if (_substrcmp(*av, "any") == 0 || 4019 add_ports(cmd, *av, proto, O_IP_SRCPORT, cblen)) { 4020 av++; 4021 if (F_LEN(cmd) != 0) 4022 cmd = next_cmd(cmd, &cblen); 4023 } 4024 } 4025 4026 /* 4027 * "to", mandatory 4028 */ 4029 if ( (av[0] == NULL) || _substrcmp(*av, "to") != 0 ) 4030 errx(EX_USAGE, "missing ``to''"); 4031 av++; 4032 4033 /* 4034 * destination, mandatory 4035 */ 4036 OR_START(dest_ip); 4037 NOT_BLOCK; /* optional "not" */ 4038 NEED1("missing dst address"); 4039 if (add_dst(cmd, *av, proto, cblen, tstate)) { 4040 av++; 4041 if (F_LEN(cmd) != 0) { /* ! any */ 4042 prev = cmd; 4043 cmd = next_cmd(cmd, &cblen); 4044 } 4045 } else 4046 errx( EX_USAGE, "bad destination address %s", *av); 4047 OR_BLOCK(dest_ip); 4048 4049 /* 4050 * dest. ports, optional 4051 */ 4052 NOT_BLOCK; /* optional "not" */ 4053 if (av[0]) { 4054 if (_substrcmp(*av, "any") == 0 || 4055 add_ports(cmd, *av, proto, O_IP_DSTPORT, cblen)) { 4056 av++; 4057 if (F_LEN(cmd) != 0) 4058 cmd = next_cmd(cmd, &cblen); 4059 } 4060 } 4061 4062 read_options: 4063 if (av[0] && first_cmd == cmd) { 4064 /* 4065 * nothing specified so far, store in the rule to ease 4066 * printout later. 4067 */ 4068 rule->flags |= IPFW_RULE_NOOPT; 4069 } 4070 prev = NULL; 4071 while ( av[0] != NULL ) { 4072 char *s; 4073 ipfw_insn_u32 *cmd32; /* alias for cmd */ 4074 4075 s = *av; 4076 cmd32 = (ipfw_insn_u32 *)cmd; 4077 4078 if (*s == '!') { /* alternate syntax for NOT */ 4079 if (cmd->len & F_NOT) 4080 errx(EX_USAGE, "double \"not\" not allowed\n"); 4081 cmd->len = F_NOT; 4082 s++; 4083 } 4084 i = match_token(rule_options, s); 4085 av++; 4086 switch(i) { 4087 case TOK_NOT: 4088 if (cmd->len & F_NOT) 4089 errx(EX_USAGE, "double \"not\" not allowed\n"); 4090 cmd->len = F_NOT; 4091 break; 4092 4093 case TOK_OR: 4094 if (open_par == 0 || prev == NULL) 4095 errx(EX_USAGE, "invalid \"or\" block\n"); 4096 prev->len |= F_OR; 4097 break; 4098 4099 case TOK_STARTBRACE: 4100 if (open_par) 4101 errx(EX_USAGE, "+nested \"(\" not allowed\n"); 4102 open_par = 1; 4103 break; 4104 4105 case TOK_ENDBRACE: 4106 if (!open_par) 4107 errx(EX_USAGE, "+missing \")\"\n"); 4108 open_par = 0; 4109 prev = NULL; 4110 break; 4111 4112 case TOK_IN: 4113 fill_cmd(cmd, O_IN, 0, 0); 4114 break; 4115 4116 case TOK_OUT: 4117 cmd->len ^= F_NOT; /* toggle F_NOT */ 4118 fill_cmd(cmd, O_IN, 0, 0); 4119 break; 4120 4121 case TOK_DIVERTED: 4122 fill_cmd(cmd, O_DIVERTED, 0, 3); 4123 break; 4124 4125 case TOK_DIVERTEDLOOPBACK: 4126 fill_cmd(cmd, O_DIVERTED, 0, 1); 4127 break; 4128 4129 case TOK_DIVERTEDOUTPUT: 4130 fill_cmd(cmd, O_DIVERTED, 0, 2); 4131 break; 4132 4133 case TOK_FRAG: 4134 fill_cmd(cmd, O_FRAG, 0, 0); 4135 break; 4136 4137 case TOK_LAYER2: 4138 fill_cmd(cmd, O_LAYER2, 0, 0); 4139 break; 4140 4141 case TOK_XMIT: 4142 case TOK_RECV: 4143 case TOK_VIA: 4144 NEED1("recv, xmit, via require interface name" 4145 " or address"); 4146 fill_iface((ipfw_insn_if *)cmd, av[0], cblen, tstate); 4147 av++; 4148 if (F_LEN(cmd) == 0) /* not a valid address */ 4149 break; 4150 if (i == TOK_XMIT) 4151 cmd->opcode = O_XMIT; 4152 else if (i == TOK_RECV) 4153 cmd->opcode = O_RECV; 4154 else if (i == TOK_VIA) 4155 cmd->opcode = O_VIA; 4156 break; 4157 4158 case TOK_ICMPTYPES: 4159 NEED1("icmptypes requires list of types"); 4160 fill_icmptypes((ipfw_insn_u32 *)cmd, *av); 4161 av++; 4162 break; 4163 4164 case TOK_ICMP6TYPES: 4165 NEED1("icmptypes requires list of types"); 4166 fill_icmp6types((ipfw_insn_icmp6 *)cmd, *av, cblen); 4167 av++; 4168 break; 4169 4170 case TOK_IPTTL: 4171 NEED1("ipttl requires TTL"); 4172 if (strpbrk(*av, "-,")) { 4173 if (!add_ports(cmd, *av, 0, O_IPTTL, cblen)) 4174 errx(EX_DATAERR, "invalid ipttl %s", *av); 4175 } else 4176 fill_cmd(cmd, O_IPTTL, 0, strtoul(*av, NULL, 0)); 4177 av++; 4178 break; 4179 4180 case TOK_IPID: 4181 NEED1("ipid requires id"); 4182 if (strpbrk(*av, "-,")) { 4183 if (!add_ports(cmd, *av, 0, O_IPID, cblen)) 4184 errx(EX_DATAERR, "invalid ipid %s", *av); 4185 } else 4186 fill_cmd(cmd, O_IPID, 0, strtoul(*av, NULL, 0)); 4187 av++; 4188 break; 4189 4190 case TOK_IPLEN: 4191 NEED1("iplen requires length"); 4192 if (strpbrk(*av, "-,")) { 4193 if (!add_ports(cmd, *av, 0, O_IPLEN, cblen)) 4194 errx(EX_DATAERR, "invalid ip len %s", *av); 4195 } else 4196 fill_cmd(cmd, O_IPLEN, 0, strtoul(*av, NULL, 0)); 4197 av++; 4198 break; 4199 4200 case TOK_IPVER: 4201 NEED1("ipver requires version"); 4202 fill_cmd(cmd, O_IPVER, 0, strtoul(*av, NULL, 0)); 4203 av++; 4204 break; 4205 4206 case TOK_IPPRECEDENCE: 4207 NEED1("ipprecedence requires value"); 4208 fill_cmd(cmd, O_IPPRECEDENCE, 0, 4209 (strtoul(*av, NULL, 0) & 7) << 5); 4210 av++; 4211 break; 4212 4213 case TOK_DSCP: 4214 NEED1("missing DSCP code"); 4215 fill_dscp(cmd, *av, cblen); 4216 av++; 4217 break; 4218 4219 case TOK_IPOPTS: 4220 NEED1("missing argument for ipoptions"); 4221 fill_flags_cmd(cmd, O_IPOPT, f_ipopts, *av); 4222 av++; 4223 break; 4224 4225 case TOK_IPTOS: 4226 NEED1("missing argument for iptos"); 4227 fill_flags_cmd(cmd, O_IPTOS, f_iptos, *av); 4228 av++; 4229 break; 4230 4231 case TOK_UID: 4232 NEED1("uid requires argument"); 4233 { 4234 char *end; 4235 uid_t uid; 4236 struct passwd *pwd; 4237 4238 cmd->opcode = O_UID; 4239 uid = strtoul(*av, &end, 0); 4240 pwd = (*end == '\0') ? getpwuid(uid) : getpwnam(*av); 4241 if (pwd == NULL) 4242 errx(EX_DATAERR, "uid \"%s\" nonexistent", *av); 4243 cmd32->d[0] = pwd->pw_uid; 4244 cmd->len |= F_INSN_SIZE(ipfw_insn_u32); 4245 av++; 4246 } 4247 break; 4248 4249 case TOK_GID: 4250 NEED1("gid requires argument"); 4251 { 4252 char *end; 4253 gid_t gid; 4254 struct group *grp; 4255 4256 cmd->opcode = O_GID; 4257 gid = strtoul(*av, &end, 0); 4258 grp = (*end == '\0') ? getgrgid(gid) : getgrnam(*av); 4259 if (grp == NULL) 4260 errx(EX_DATAERR, "gid \"%s\" nonexistent", *av); 4261 cmd32->d[0] = grp->gr_gid; 4262 cmd->len |= F_INSN_SIZE(ipfw_insn_u32); 4263 av++; 4264 } 4265 break; 4266 4267 case TOK_JAIL: 4268 NEED1("jail requires argument"); 4269 { 4270 char *end; 4271 int jid; 4272 4273 cmd->opcode = O_JAIL; 4274 jid = (int)strtol(*av, &end, 0); 4275 if (jid < 0 || *end != '\0') 4276 errx(EX_DATAERR, "jail requires prison ID"); 4277 cmd32->d[0] = (uint32_t)jid; 4278 cmd->len |= F_INSN_SIZE(ipfw_insn_u32); 4279 av++; 4280 } 4281 break; 4282 4283 case TOK_ESTAB: 4284 fill_cmd(cmd, O_ESTAB, 0, 0); 4285 break; 4286 4287 case TOK_SETUP: 4288 fill_cmd(cmd, O_TCPFLAGS, 0, 4289 (TH_SYN) | ( (TH_ACK) & 0xff) <<8 ); 4290 break; 4291 4292 case TOK_TCPDATALEN: 4293 NEED1("tcpdatalen requires length"); 4294 if (strpbrk(*av, "-,")) { 4295 if (!add_ports(cmd, *av, 0, O_TCPDATALEN, cblen)) 4296 errx(EX_DATAERR, "invalid tcpdata len %s", *av); 4297 } else 4298 fill_cmd(cmd, O_TCPDATALEN, 0, 4299 strtoul(*av, NULL, 0)); 4300 av++; 4301 break; 4302 4303 case TOK_TCPOPTS: 4304 NEED1("missing argument for tcpoptions"); 4305 fill_flags_cmd(cmd, O_TCPOPTS, f_tcpopts, *av); 4306 av++; 4307 break; 4308 4309 case TOK_TCPSEQ: 4310 case TOK_TCPACK: 4311 NEED1("tcpseq/tcpack requires argument"); 4312 cmd->len = F_INSN_SIZE(ipfw_insn_u32); 4313 cmd->opcode = (i == TOK_TCPSEQ) ? O_TCPSEQ : O_TCPACK; 4314 cmd32->d[0] = htonl(strtoul(*av, NULL, 0)); 4315 av++; 4316 break; 4317 4318 case TOK_TCPWIN: 4319 NEED1("tcpwin requires length"); 4320 if (strpbrk(*av, "-,")) { 4321 if (!add_ports(cmd, *av, 0, O_TCPWIN, cblen)) 4322 errx(EX_DATAERR, "invalid tcpwin len %s", *av); 4323 } else 4324 fill_cmd(cmd, O_TCPWIN, 0, 4325 strtoul(*av, NULL, 0)); 4326 av++; 4327 break; 4328 4329 case TOK_TCPFLAGS: 4330 NEED1("missing argument for tcpflags"); 4331 cmd->opcode = O_TCPFLAGS; 4332 fill_flags_cmd(cmd, O_TCPFLAGS, f_tcpflags, *av); 4333 av++; 4334 break; 4335 4336 case TOK_KEEPSTATE: 4337 if (open_par) 4338 errx(EX_USAGE, "keep-state cannot be part " 4339 "of an or block"); 4340 if (have_state) 4341 errx(EX_USAGE, "only one of keep-state " 4342 "and limit is allowed"); 4343 have_state = cmd; 4344 fill_cmd(cmd, O_KEEP_STATE, 0, 0); 4345 break; 4346 4347 case TOK_LIMIT: { 4348 ipfw_insn_limit *c = (ipfw_insn_limit *)cmd; 4349 int val; 4350 4351 if (open_par) 4352 errx(EX_USAGE, 4353 "limit cannot be part of an or block"); 4354 if (have_state) 4355 errx(EX_USAGE, "only one of keep-state and " 4356 "limit is allowed"); 4357 have_state = cmd; 4358 4359 cmd->len = F_INSN_SIZE(ipfw_insn_limit); 4360 CHECK_CMDLEN; 4361 cmd->opcode = O_LIMIT; 4362 c->limit_mask = c->conn_limit = 0; 4363 4364 while ( av[0] != NULL ) { 4365 if ((val = match_token(limit_masks, *av)) <= 0) 4366 break; 4367 c->limit_mask |= val; 4368 av++; 4369 } 4370 4371 if (c->limit_mask == 0) 4372 errx(EX_USAGE, "limit: missing limit mask"); 4373 4374 GET_UINT_ARG(c->conn_limit, IPFW_ARG_MIN, IPFW_ARG_MAX, 4375 TOK_LIMIT, rule_options); 4376 4377 av++; 4378 break; 4379 } 4380 4381 case TOK_PROTO: 4382 NEED1("missing protocol"); 4383 if (add_proto(cmd, *av, &proto)) { 4384 av++; 4385 } else 4386 errx(EX_DATAERR, "invalid protocol ``%s''", 4387 *av); 4388 break; 4389 4390 case TOK_SRCIP: 4391 NEED1("missing source IP"); 4392 if (add_srcip(cmd, *av, cblen, tstate)) { 4393 av++; 4394 } 4395 break; 4396 4397 case TOK_DSTIP: 4398 NEED1("missing destination IP"); 4399 if (add_dstip(cmd, *av, cblen, tstate)) { 4400 av++; 4401 } 4402 break; 4403 4404 case TOK_SRCIP6: 4405 NEED1("missing source IP6"); 4406 if (add_srcip6(cmd, *av, cblen)) { 4407 av++; 4408 } 4409 break; 4410 4411 case TOK_DSTIP6: 4412 NEED1("missing destination IP6"); 4413 if (add_dstip6(cmd, *av, cblen)) { 4414 av++; 4415 } 4416 break; 4417 4418 case TOK_SRCPORT: 4419 NEED1("missing source port"); 4420 if (_substrcmp(*av, "any") == 0 || 4421 add_ports(cmd, *av, proto, O_IP_SRCPORT, cblen)) { 4422 av++; 4423 } else 4424 errx(EX_DATAERR, "invalid source port %s", *av); 4425 break; 4426 4427 case TOK_DSTPORT: 4428 NEED1("missing destination port"); 4429 if (_substrcmp(*av, "any") == 0 || 4430 add_ports(cmd, *av, proto, O_IP_DSTPORT, cblen)) { 4431 av++; 4432 } else 4433 errx(EX_DATAERR, "invalid destination port %s", 4434 *av); 4435 break; 4436 4437 case TOK_MAC: 4438 if (add_mac(cmd, av, cblen)) 4439 av += 2; 4440 break; 4441 4442 case TOK_MACTYPE: 4443 NEED1("missing mac type"); 4444 if (!add_mactype(cmd, *av, cblen)) 4445 errx(EX_DATAERR, "invalid mac type %s", *av); 4446 av++; 4447 break; 4448 4449 case TOK_VERREVPATH: 4450 fill_cmd(cmd, O_VERREVPATH, 0, 0); 4451 break; 4452 4453 case TOK_VERSRCREACH: 4454 fill_cmd(cmd, O_VERSRCREACH, 0, 0); 4455 break; 4456 4457 case TOK_ANTISPOOF: 4458 fill_cmd(cmd, O_ANTISPOOF, 0, 0); 4459 break; 4460 4461 case TOK_IPSEC: 4462 fill_cmd(cmd, O_IPSEC, 0, 0); 4463 break; 4464 4465 case TOK_IPV6: 4466 fill_cmd(cmd, O_IP6, 0, 0); 4467 break; 4468 4469 case TOK_IPV4: 4470 fill_cmd(cmd, O_IP4, 0, 0); 4471 break; 4472 4473 case TOK_EXT6HDR: 4474 fill_ext6hdr( cmd, *av ); 4475 av++; 4476 break; 4477 4478 case TOK_FLOWID: 4479 if (proto != IPPROTO_IPV6 ) 4480 errx( EX_USAGE, "flow-id filter is active " 4481 "only for ipv6 protocol\n"); 4482 fill_flow6( (ipfw_insn_u32 *) cmd, *av, cblen); 4483 av++; 4484 break; 4485 4486 case TOK_COMMENT: 4487 fill_comment(cmd, av, cblen); 4488 av[0]=NULL; 4489 break; 4490 4491 case TOK_TAGGED: 4492 if (av[0] && strpbrk(*av, "-,")) { 4493 if (!add_ports(cmd, *av, 0, O_TAGGED, cblen)) 4494 errx(EX_DATAERR, "tagged: invalid tag" 4495 " list: %s", *av); 4496 } 4497 else { 4498 uint16_t tag; 4499 4500 GET_UINT_ARG(tag, IPFW_ARG_MIN, IPFW_ARG_MAX, 4501 TOK_TAGGED, rule_options); 4502 fill_cmd(cmd, O_TAGGED, 0, tag); 4503 } 4504 av++; 4505 break; 4506 4507 case TOK_FIB: 4508 NEED1("fib requires fib number"); 4509 fill_cmd(cmd, O_FIB, 0, strtoul(*av, NULL, 0)); 4510 av++; 4511 break; 4512 case TOK_SOCKARG: 4513 fill_cmd(cmd, O_SOCKARG, 0, 0); 4514 break; 4515 4516 case TOK_LOOKUP: { 4517 ipfw_insn_u32 *c = (ipfw_insn_u32 *)cmd; 4518 int j; 4519 4520 if (!av[0] || !av[1]) 4521 errx(EX_USAGE, "format: lookup argument tablenum"); 4522 cmd->opcode = O_IP_DST_LOOKUP; 4523 cmd->len |= F_INSN_SIZE(ipfw_insn) + 2; 4524 i = match_token(rule_options, *av); 4525 for (j = 0; lookup_key[j] >= 0 ; j++) { 4526 if (i == lookup_key[j]) 4527 break; 4528 } 4529 if (lookup_key[j] <= 0) 4530 errx(EX_USAGE, "format: cannot lookup on %s", *av); 4531 __PAST_END(c->d, 1) = j; // i converted to option 4532 av++; 4533 4534 if ((j = pack_table(tstate, *av)) == 0) 4535 errx(EX_DATAERR, "Invalid table name: %s", *av); 4536 4537 cmd->arg1 = j; 4538 av++; 4539 } 4540 break; 4541 case TOK_FLOW: 4542 NEED1("missing table name"); 4543 if (strncmp(*av, "table(", 6) != 0) 4544 errx(EX_DATAERR, 4545 "enclose table name into \"table()\""); 4546 fill_table(cmd, *av, O_IP_FLOW_LOOKUP, tstate); 4547 av++; 4548 break; 4549 4550 default: 4551 errx(EX_USAGE, "unrecognised option [%d] %s\n", i, s); 4552 } 4553 if (F_LEN(cmd) > 0) { /* prepare to advance */ 4554 prev = cmd; 4555 cmd = next_cmd(cmd, &cblen); 4556 } 4557 } 4558 4559 done: 4560 /* 4561 * Now copy stuff into the rule. 4562 * If we have a keep-state option, the first instruction 4563 * must be a PROBE_STATE (which is generated here). 4564 * If we have a LOG option, it was stored as the first command, 4565 * and now must be moved to the top of the action part. 4566 */ 4567 dst = (ipfw_insn *)rule->cmd; 4568 4569 /* 4570 * First thing to write into the command stream is the match probability. 4571 */ 4572 if (match_prob != 1) { /* 1 means always match */ 4573 dst->opcode = O_PROB; 4574 dst->len = 2; 4575 *((int32_t *)(dst+1)) = (int32_t)(match_prob * 0x7fffffff); 4576 dst += dst->len; 4577 } 4578 4579 /* 4580 * generate O_PROBE_STATE if necessary 4581 */ 4582 if (have_state && have_state->opcode != O_CHECK_STATE) { 4583 fill_cmd(dst, O_PROBE_STATE, 0, 0); 4584 dst = next_cmd(dst, &rblen); 4585 } 4586 4587 /* copy all commands but O_LOG, O_KEEP_STATE, O_LIMIT, O_ALTQ, O_TAG */ 4588 for (src = (ipfw_insn *)cmdbuf; src != cmd; src += i) { 4589 i = F_LEN(src); 4590 CHECK_RBUFLEN(i); 4591 4592 switch (src->opcode) { 4593 case O_LOG: 4594 case O_KEEP_STATE: 4595 case O_LIMIT: 4596 case O_ALTQ: 4597 case O_TAG: 4598 break; 4599 default: 4600 bcopy(src, dst, i * sizeof(uint32_t)); 4601 dst += i; 4602 } 4603 } 4604 4605 /* 4606 * put back the have_state command as last opcode 4607 */ 4608 if (have_state && have_state->opcode != O_CHECK_STATE) { 4609 i = F_LEN(have_state); 4610 CHECK_RBUFLEN(i); 4611 bcopy(have_state, dst, i * sizeof(uint32_t)); 4612 dst += i; 4613 } 4614 /* 4615 * start action section 4616 */ 4617 rule->act_ofs = dst - rule->cmd; 4618 4619 /* put back O_LOG, O_ALTQ, O_TAG if necessary */ 4620 if (have_log) { 4621 i = F_LEN(have_log); 4622 CHECK_RBUFLEN(i); 4623 bcopy(have_log, dst, i * sizeof(uint32_t)); 4624 dst += i; 4625 } 4626 if (have_altq) { 4627 i = F_LEN(have_altq); 4628 CHECK_RBUFLEN(i); 4629 bcopy(have_altq, dst, i * sizeof(uint32_t)); 4630 dst += i; 4631 } 4632 if (have_tag) { 4633 i = F_LEN(have_tag); 4634 CHECK_RBUFLEN(i); 4635 bcopy(have_tag, dst, i * sizeof(uint32_t)); 4636 dst += i; 4637 } 4638 4639 /* 4640 * copy all other actions 4641 */ 4642 for (src = (ipfw_insn *)actbuf; src != action; src += i) { 4643 i = F_LEN(src); 4644 CHECK_RBUFLEN(i); 4645 bcopy(src, dst, i * sizeof(uint32_t)); 4646 dst += i; 4647 } 4648 4649 rule->cmd_len = (uint32_t *)dst - (uint32_t *)(rule->cmd); 4650 *rbufsize = (char *)dst - (char *)rule; 4651 } 4652 4653 /* 4654 * Adds one or more rules to ipfw chain. 4655 * Data layout: 4656 * Request: 4657 * [ 4658 * ip_fw3_opheader 4659 * [ ipfw_obj_ctlv(IPFW_TLV_TBL_LIST) ipfw_obj_ntlv x N ] (optional *1) 4660 * [ ipfw_obj_ctlv(IPFW_TLV_RULE_LIST) [ ip_fw_rule ip_fw_insn ] x N ] (*2) (*3) 4661 * ] 4662 * Reply: 4663 * [ 4664 * ip_fw3_opheader 4665 * [ ipfw_obj_ctlv(IPFW_TLV_TBL_LIST) ipfw_obj_ntlv x N ] (optional) 4666 * [ ipfw_obj_ctlv(IPFW_TLV_RULE_LIST) [ ip_fw_rule ip_fw_insn ] x N ] 4667 * ] 4668 * 4669 * Rules in reply are modified to store their actual ruleset number. 4670 * 4671 * (*1) TLVs inside IPFW_TLV_TBL_LIST needs to be sorted ascending 4672 * accoring to their idx field and there has to be no duplicates. 4673 * (*2) Numbered rules inside IPFW_TLV_RULE_LIST needs to be sorted ascending. 4674 * (*3) Each ip_fw structure needs to be aligned to u64 boundary. 4675 */ 4676 void 4677 ipfw_add(char *av[]) 4678 { 4679 uint32_t rulebuf[1024]; 4680 int rbufsize, default_off, tlen, rlen; 4681 size_t sz; 4682 struct tidx ts; 4683 struct ip_fw_rule *rule; 4684 caddr_t tbuf; 4685 ip_fw3_opheader *op3; 4686 ipfw_obj_ctlv *ctlv, *tstate; 4687 4688 rbufsize = sizeof(rulebuf); 4689 memset(rulebuf, 0, rbufsize); 4690 memset(&ts, 0, sizeof(ts)); 4691 4692 /* Optimize case with no tables */ 4693 default_off = sizeof(ipfw_obj_ctlv) + sizeof(ip_fw3_opheader); 4694 op3 = (ip_fw3_opheader *)rulebuf; 4695 ctlv = (ipfw_obj_ctlv *)(op3 + 1); 4696 rule = (struct ip_fw_rule *)(ctlv + 1); 4697 rbufsize -= default_off; 4698 4699 compile_rule(av, (uint32_t *)rule, &rbufsize, &ts); 4700 /* Align rule size to u64 boundary */ 4701 rlen = roundup2(rbufsize, sizeof(uint64_t)); 4702 4703 tbuf = NULL; 4704 sz = 0; 4705 tstate = NULL; 4706 if (ts.count != 0) { 4707 /* Some tables. We have to alloc more data */ 4708 tlen = ts.count * sizeof(ipfw_obj_ntlv); 4709 sz = default_off + sizeof(ipfw_obj_ctlv) + tlen + rlen; 4710 4711 if ((tbuf = calloc(1, sz)) == NULL) 4712 err(EX_UNAVAILABLE, "malloc() failed for IP_FW_ADD"); 4713 op3 = (ip_fw3_opheader *)tbuf; 4714 /* Tables first */ 4715 ctlv = (ipfw_obj_ctlv *)(op3 + 1); 4716 ctlv->head.type = IPFW_TLV_TBLNAME_LIST; 4717 ctlv->head.length = sizeof(ipfw_obj_ctlv) + tlen; 4718 ctlv->count = ts.count; 4719 ctlv->objsize = sizeof(ipfw_obj_ntlv); 4720 memcpy(ctlv + 1, ts.idx, tlen); 4721 table_sort_ctlv(ctlv); 4722 tstate = ctlv; 4723 /* Rule next */ 4724 ctlv = (ipfw_obj_ctlv *)((caddr_t)ctlv + ctlv->head.length); 4725 ctlv->head.type = IPFW_TLV_RULE_LIST; 4726 ctlv->head.length = sizeof(ipfw_obj_ctlv) + rlen; 4727 ctlv->count = 1; 4728 memcpy(ctlv + 1, rule, rbufsize); 4729 } else { 4730 /* Simply add header */ 4731 sz = rlen + default_off; 4732 memset(ctlv, 0, sizeof(*ctlv)); 4733 ctlv->head.type = IPFW_TLV_RULE_LIST; 4734 ctlv->head.length = sizeof(ipfw_obj_ctlv) + rlen; 4735 ctlv->count = 1; 4736 } 4737 4738 if (do_get3(IP_FW_XADD, op3, &sz) != 0) 4739 err(EX_UNAVAILABLE, "getsockopt(%s)", "IP_FW_XADD"); 4740 4741 if (!co.do_quiet) { 4742 struct format_opts sfo; 4743 struct buf_pr bp; 4744 memset(&sfo, 0, sizeof(sfo)); 4745 sfo.tstate = tstate; 4746 sfo.set_mask = (uint32_t)(-1); 4747 bp_alloc(&bp, 4096); 4748 show_static_rule(&co, &sfo, &bp, rule, NULL); 4749 printf("%s", bp.buf); 4750 bp_free(&bp); 4751 } 4752 4753 if (tbuf != NULL) 4754 free(tbuf); 4755 4756 if (ts.idx != NULL) 4757 free(ts.idx); 4758 } 4759 4760 /* 4761 * clear the counters or the log counters. 4762 * optname has the following values: 4763 * 0 (zero both counters and logging) 4764 * 1 (zero logging only) 4765 */ 4766 void 4767 ipfw_zero(int ac, char *av[], int optname) 4768 { 4769 ipfw_range_tlv rt; 4770 uint32_t arg; 4771 int failed = EX_OK; 4772 char const *errstr; 4773 char const *name = optname ? "RESETLOG" : "ZERO"; 4774 4775 optname = optname ? IP_FW_XRESETLOG : IP_FW_XZERO; 4776 memset(&rt, 0, sizeof(rt)); 4777 4778 av++; ac--; 4779 4780 if (ac == 0) { 4781 /* clear all entries */ 4782 rt.flags = IPFW_RCFLAG_ALL; 4783 if (do_range_cmd(optname, &rt) < 0) 4784 err(EX_UNAVAILABLE, "setsockopt(IP_FW_X%s)", name); 4785 if (!co.do_quiet) 4786 printf("%s.\n", optname == IP_FW_XZERO ? 4787 "Accounting cleared":"Logging counts reset"); 4788 4789 return; 4790 } 4791 4792 while (ac) { 4793 /* Rule number */ 4794 if (isdigit(**av)) { 4795 arg = strtonum(*av, 0, 0xffff, &errstr); 4796 if (errstr) 4797 errx(EX_DATAERR, 4798 "invalid rule number %s\n", *av); 4799 rt.start_rule = arg; 4800 rt.end_rule = arg; 4801 rt.flags |= IPFW_RCFLAG_RANGE; 4802 if (co.use_set != 0) { 4803 rt.set = co.use_set - 1; 4804 rt.flags |= IPFW_RCFLAG_SET; 4805 } 4806 if (do_range_cmd(optname, &rt) != 0) { 4807 warn("rule %u: setsockopt(IP_FW_X%s)", 4808 arg, name); 4809 failed = EX_UNAVAILABLE; 4810 } else if (rt.new_set == 0) { 4811 printf("Entry %d not found\n", arg); 4812 failed = EX_UNAVAILABLE; 4813 } else if (!co.do_quiet) 4814 printf("Entry %d %s.\n", arg, 4815 optname == IP_FW_XZERO ? 4816 "cleared" : "logging count reset"); 4817 } else { 4818 errx(EX_USAGE, "invalid rule number ``%s''", *av); 4819 } 4820 av++; ac--; 4821 } 4822 if (failed != EX_OK) 4823 exit(failed); 4824 } 4825 4826 void 4827 ipfw_flush(int force) 4828 { 4829 ipfw_range_tlv rt; 4830 4831 if (!force && !co.do_quiet) { /* need to ask user */ 4832 int c; 4833 4834 printf("Are you sure? [yn] "); 4835 fflush(stdout); 4836 do { 4837 c = toupper(getc(stdin)); 4838 while (c != '\n' && getc(stdin) != '\n') 4839 if (feof(stdin)) 4840 return; /* and do not flush */ 4841 } while (c != 'Y' && c != 'N'); 4842 printf("\n"); 4843 if (c == 'N') /* user said no */ 4844 return; 4845 } 4846 if (co.do_pipe) { 4847 dummynet_flush(); 4848 return; 4849 } 4850 /* `ipfw set N flush` - is the same that `ipfw delete set N` */ 4851 memset(&rt, 0, sizeof(rt)); 4852 if (co.use_set != 0) { 4853 rt.set = co.use_set - 1; 4854 rt.flags = IPFW_RCFLAG_SET; 4855 } else 4856 rt.flags = IPFW_RCFLAG_ALL; 4857 if (do_range_cmd(IP_FW_XDEL, &rt) != 0) 4858 err(EX_UNAVAILABLE, "setsockopt(IP_FW_XDEL)"); 4859 if (!co.do_quiet) 4860 printf("Flushed all %s.\n", co.do_pipe ? "pipes" : "rules"); 4861 } 4862 4863 static struct _s_x intcmds[] = { 4864 { "talist", TOK_TALIST }, 4865 { "iflist", TOK_IFLIST }, 4866 { "vlist", TOK_VLIST }, 4867 { NULL, 0 } 4868 }; 4869 4870 void 4871 ipfw_internal_handler(int ac, char *av[]) 4872 { 4873 int tcmd; 4874 4875 ac--; av++; 4876 NEED1("internal cmd required"); 4877 4878 if ((tcmd = match_token(intcmds, *av)) == -1) 4879 errx(EX_USAGE, "invalid internal sub-cmd: %s", *av); 4880 4881 switch (tcmd) { 4882 case TOK_IFLIST: 4883 ipfw_list_tifaces(); 4884 break; 4885 case TOK_TALIST: 4886 ipfw_list_ta(ac, av); 4887 break; 4888 case TOK_VLIST: 4889 ipfw_list_values(ac, av); 4890 break; 4891 } 4892 } 4893 4894 static int 4895 ipfw_get_tracked_ifaces(ipfw_obj_lheader **polh) 4896 { 4897 ipfw_obj_lheader req, *olh; 4898 size_t sz; 4899 4900 memset(&req, 0, sizeof(req)); 4901 sz = sizeof(req); 4902 4903 if (do_get3(IP_FW_XIFLIST, &req.opheader, &sz) != 0) { 4904 if (errno != ENOMEM) 4905 return (errno); 4906 } 4907 4908 sz = req.size; 4909 if ((olh = calloc(1, sz)) == NULL) 4910 return (ENOMEM); 4911 4912 olh->size = sz; 4913 if (do_get3(IP_FW_XIFLIST, &olh->opheader, &sz) != 0) { 4914 free(olh); 4915 return (errno); 4916 } 4917 4918 *polh = olh; 4919 return (0); 4920 } 4921 4922 static int 4923 ifinfo_cmp(const void *a, const void *b) 4924 { 4925 ipfw_iface_info *ia, *ib; 4926 4927 ia = (ipfw_iface_info *)a; 4928 ib = (ipfw_iface_info *)b; 4929 4930 return (stringnum_cmp(ia->ifname, ib->ifname)); 4931 } 4932 4933 /* 4934 * Retrieves table list from kernel, 4935 * optionally sorts it and calls requested function for each table. 4936 * Returns 0 on success. 4937 */ 4938 static void 4939 ipfw_list_tifaces() 4940 { 4941 ipfw_obj_lheader *olh; 4942 ipfw_iface_info *info; 4943 int i, error; 4944 4945 if ((error = ipfw_get_tracked_ifaces(&olh)) != 0) 4946 err(EX_OSERR, "Unable to request ipfw tracked interface list"); 4947 4948 4949 qsort(olh + 1, olh->count, olh->objsize, ifinfo_cmp); 4950 4951 info = (ipfw_iface_info *)(olh + 1); 4952 for (i = 0; i < olh->count; i++) { 4953 if (info->flags & IPFW_IFFLAG_RESOLVED) 4954 printf("%s ifindex: %d refcount: %u changes: %u\n", 4955 info->ifname, info->ifindex, info->refcnt, 4956 info->gencnt); 4957 else 4958 printf("%s ifindex: unresolved refcount: %u changes: %u\n", 4959 info->ifname, info->refcnt, info->gencnt); 4960 info = (ipfw_iface_info *)((caddr_t)info + olh->objsize); 4961 } 4962 4963 free(olh); 4964 } 4965 4966 4967 4968 4969