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