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