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