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