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