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