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