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