ipfw2.c revision 158879
1/* 2 * Copyright (c) 2002-2003 Luigi Rizzo 3 * Copyright (c) 1996 Alex Nash, Paul Traina, Poul-Henning Kamp 4 * Copyright (c) 1994 Ugen J.S.Antsilevich 5 * 6 * Idea and grammar partially left from: 7 * Copyright (c) 1993 Daniel Boulet 8 * 9 * Redistribution and use in source forms, with and without modification, 10 * are permitted provided that this entire comment appears intact. 11 * 12 * Redistribution in binary form may occur without any restrictions. 13 * Obviously, it would be nice if you gave credit where credit is due 14 * but requiring it would be too onerous. 15 * 16 * This software is provided ``AS IS'' without any warranties of any kind. 17 * 18 * NEW command line interface for IP firewall facility 19 * 20 * $FreeBSD: head/sbin/ipfw/ipfw2.c 158879 2006-05-24 13:09:55Z oleg $ 21 */ 22 23#include <sys/param.h> 24#include <sys/mbuf.h> 25#include <sys/socket.h> 26#include <sys/sockio.h> 27#include <sys/sysctl.h> 28#include <sys/time.h> 29#include <sys/wait.h> 30#include <sys/queue.h> 31 32#include <ctype.h> 33#include <err.h> 34#include <errno.h> 35#include <grp.h> 36#include <limits.h> 37#include <netdb.h> 38#include <pwd.h> 39#include <signal.h> 40#include <stdio.h> 41#include <stdlib.h> 42#include <stdarg.h> 43#include <string.h> 44#include <timeconv.h> /* XXX do we need this ? */ 45#include <unistd.h> 46#include <sysexits.h> 47#include <unistd.h> 48#include <fcntl.h> 49 50#include <net/if.h> 51#include <net/pfvar.h> 52#include <net/route.h> /* def. of struct route */ 53#include <netinet/in.h> 54#include <netinet/in_systm.h> 55#include <netinet/ip.h> 56#include <netinet/ip_icmp.h> 57#include <netinet/icmp6.h> 58#include <netinet/ip_fw.h> 59#include <netinet/ip_dummynet.h> 60#include <netinet/tcp.h> 61#include <arpa/inet.h> 62 63int 64 do_resolv, /* Would try to resolve all */ 65 do_time, /* Show time stamps */ 66 do_quiet, /* Be quiet in add and flush */ 67 do_pipe, /* this cmd refers to a pipe */ 68 do_sort, /* field to sort results (0 = no) */ 69 do_dynamic, /* display dynamic rules */ 70 do_expired, /* display expired dynamic rules */ 71 do_compact, /* show rules in compact mode */ 72 do_force, /* do not ask for confirmation */ 73 show_sets, /* display rule sets */ 74 test_only, /* only check syntax */ 75 comment_only, /* only print action and comment */ 76 verbose; 77 78#define IP_MASK_ALL 0xffffffff 79/* 80 * the following macro returns an error message if we run out of 81 * arguments. 82 */ 83#define NEED1(msg) {if (!ac) errx(EX_USAGE, msg);} 84 85#define NOT_NUMBER(str, msg) do { \ 86 char *c; \ 87 for (c = str; *c != '\0'; c++) { \ 88 if (isdigit(*c)) \ 89 continue; \ 90 errx(EX_DATAERR, msg); \ 91 } \ 92} while (0) 93 94/* 95 * _s_x is a structure that stores a string <-> token pairs, used in 96 * various places in the parser. Entries are stored in arrays, 97 * with an entry with s=NULL as terminator. 98 * The search routines are match_token() and match_value(). 99 * Often, an element with x=0 contains an error string. 100 * 101 */ 102struct _s_x { 103 char const *s; 104 int x; 105}; 106 107static struct _s_x f_tcpflags[] = { 108 { "syn", TH_SYN }, 109 { "fin", TH_FIN }, 110 { "ack", TH_ACK }, 111 { "psh", TH_PUSH }, 112 { "rst", TH_RST }, 113 { "urg", TH_URG }, 114 { "tcp flag", 0 }, 115 { NULL, 0 } 116}; 117 118static struct _s_x f_tcpopts[] = { 119 { "mss", IP_FW_TCPOPT_MSS }, 120 { "maxseg", IP_FW_TCPOPT_MSS }, 121 { "window", IP_FW_TCPOPT_WINDOW }, 122 { "sack", IP_FW_TCPOPT_SACK }, 123 { "ts", IP_FW_TCPOPT_TS }, 124 { "timestamp", IP_FW_TCPOPT_TS }, 125 { "cc", IP_FW_TCPOPT_CC }, 126 { "tcp option", 0 }, 127 { NULL, 0 } 128}; 129 130/* 131 * IP options span the range 0 to 255 so we need to remap them 132 * (though in fact only the low 5 bits are significant). 133 */ 134static struct _s_x f_ipopts[] = { 135 { "ssrr", IP_FW_IPOPT_SSRR}, 136 { "lsrr", IP_FW_IPOPT_LSRR}, 137 { "rr", IP_FW_IPOPT_RR}, 138 { "ts", IP_FW_IPOPT_TS}, 139 { "ip option", 0 }, 140 { NULL, 0 } 141}; 142 143static struct _s_x f_iptos[] = { 144 { "lowdelay", IPTOS_LOWDELAY}, 145 { "throughput", IPTOS_THROUGHPUT}, 146 { "reliability", IPTOS_RELIABILITY}, 147 { "mincost", IPTOS_MINCOST}, 148 { "congestion", IPTOS_CE}, 149 { "ecntransport", IPTOS_ECT}, 150 { "ip tos option", 0}, 151 { NULL, 0 } 152}; 153 154static struct _s_x limit_masks[] = { 155 {"all", DYN_SRC_ADDR|DYN_SRC_PORT|DYN_DST_ADDR|DYN_DST_PORT}, 156 {"src-addr", DYN_SRC_ADDR}, 157 {"src-port", DYN_SRC_PORT}, 158 {"dst-addr", DYN_DST_ADDR}, 159 {"dst-port", DYN_DST_PORT}, 160 {NULL, 0} 161}; 162 163/* 164 * we use IPPROTO_ETHERTYPE as a fake protocol id to call the print routines 165 * This is only used in this code. 166 */ 167#define IPPROTO_ETHERTYPE 0x1000 168static struct _s_x ether_types[] = { 169 /* 170 * Note, we cannot use "-:&/" in the names because they are field 171 * separators in the type specifications. Also, we use s = NULL as 172 * end-delimiter, because a type of 0 can be legal. 173 */ 174 { "ip", 0x0800 }, 175 { "ipv4", 0x0800 }, 176 { "ipv6", 0x86dd }, 177 { "arp", 0x0806 }, 178 { "rarp", 0x8035 }, 179 { "vlan", 0x8100 }, 180 { "loop", 0x9000 }, 181 { "trail", 0x1000 }, 182 { "at", 0x809b }, 183 { "atalk", 0x809b }, 184 { "aarp", 0x80f3 }, 185 { "pppoe_disc", 0x8863 }, 186 { "pppoe_sess", 0x8864 }, 187 { "ipx_8022", 0x00E0 }, 188 { "ipx_8023", 0x0000 }, 189 { "ipx_ii", 0x8137 }, 190 { "ipx_snap", 0x8137 }, 191 { "ipx", 0x8137 }, 192 { "ns", 0x0600 }, 193 { NULL, 0 } 194}; 195 196static void show_usage(void); 197 198enum tokens { 199 TOK_NULL=0, 200 201 TOK_OR, 202 TOK_NOT, 203 TOK_STARTBRACE, 204 TOK_ENDBRACE, 205 206 TOK_ACCEPT, 207 TOK_COUNT, 208 TOK_PIPE, 209 TOK_QUEUE, 210 TOK_DIVERT, 211 TOK_TEE, 212 TOK_NETGRAPH, 213 TOK_NGTEE, 214 TOK_FORWARD, 215 TOK_SKIPTO, 216 TOK_DENY, 217 TOK_REJECT, 218 TOK_RESET, 219 TOK_UNREACH, 220 TOK_CHECKSTATE, 221 222 TOK_ALTQ, 223 TOK_LOG, 224 TOK_TAG, 225 TOK_UNTAG, 226 227 TOK_TAGGED, 228 TOK_UID, 229 TOK_GID, 230 TOK_JAIL, 231 TOK_IN, 232 TOK_LIMIT, 233 TOK_KEEPSTATE, 234 TOK_LAYER2, 235 TOK_OUT, 236 TOK_DIVERTED, 237 TOK_DIVERTEDLOOPBACK, 238 TOK_DIVERTEDOUTPUT, 239 TOK_XMIT, 240 TOK_RECV, 241 TOK_VIA, 242 TOK_FRAG, 243 TOK_IPOPTS, 244 TOK_IPLEN, 245 TOK_IPID, 246 TOK_IPPRECEDENCE, 247 TOK_IPTOS, 248 TOK_IPTTL, 249 TOK_IPVER, 250 TOK_ESTAB, 251 TOK_SETUP, 252 TOK_TCPDATALEN, 253 TOK_TCPFLAGS, 254 TOK_TCPOPTS, 255 TOK_TCPSEQ, 256 TOK_TCPACK, 257 TOK_TCPWIN, 258 TOK_ICMPTYPES, 259 TOK_MAC, 260 TOK_MACTYPE, 261 TOK_VERREVPATH, 262 TOK_VERSRCREACH, 263 TOK_ANTISPOOF, 264 TOK_IPSEC, 265 TOK_COMMENT, 266 267 TOK_PLR, 268 TOK_NOERROR, 269 TOK_BUCKETS, 270 TOK_DSTIP, 271 TOK_SRCIP, 272 TOK_DSTPORT, 273 TOK_SRCPORT, 274 TOK_ALL, 275 TOK_MASK, 276 TOK_BW, 277 TOK_DELAY, 278 TOK_RED, 279 TOK_GRED, 280 TOK_DROPTAIL, 281 TOK_PROTO, 282 TOK_WEIGHT, 283 284 TOK_IPV6, 285 TOK_FLOWID, 286 TOK_ICMP6TYPES, 287 TOK_EXT6HDR, 288 TOK_DSTIP6, 289 TOK_SRCIP6, 290 291 TOK_IPV4, 292 TOK_UNREACH6, 293 TOK_RESET6, 294}; 295 296struct _s_x dummynet_params[] = { 297 { "plr", TOK_PLR }, 298 { "noerror", TOK_NOERROR }, 299 { "buckets", TOK_BUCKETS }, 300 { "dst-ip", TOK_DSTIP }, 301 { "src-ip", TOK_SRCIP }, 302 { "dst-port", TOK_DSTPORT }, 303 { "src-port", TOK_SRCPORT }, 304 { "proto", TOK_PROTO }, 305 { "weight", TOK_WEIGHT }, 306 { "all", TOK_ALL }, 307 { "mask", TOK_MASK }, 308 { "droptail", TOK_DROPTAIL }, 309 { "red", TOK_RED }, 310 { "gred", TOK_GRED }, 311 { "bw", TOK_BW }, 312 { "bandwidth", TOK_BW }, 313 { "delay", TOK_DELAY }, 314 { "pipe", TOK_PIPE }, 315 { "queue", TOK_QUEUE }, 316 { "flow-id", TOK_FLOWID}, 317 { "dst-ipv6", TOK_DSTIP6}, 318 { "dst-ip6", TOK_DSTIP6}, 319 { "src-ipv6", TOK_SRCIP6}, 320 { "src-ip6", TOK_SRCIP6}, 321 { "dummynet-params", TOK_NULL }, 322 { NULL, 0 } /* terminator */ 323}; 324 325struct _s_x rule_actions[] = { 326 { "accept", TOK_ACCEPT }, 327 { "pass", TOK_ACCEPT }, 328 { "allow", TOK_ACCEPT }, 329 { "permit", TOK_ACCEPT }, 330 { "count", TOK_COUNT }, 331 { "pipe", TOK_PIPE }, 332 { "queue", TOK_QUEUE }, 333 { "divert", TOK_DIVERT }, 334 { "tee", TOK_TEE }, 335 { "netgraph", TOK_NETGRAPH }, 336 { "ngtee", TOK_NGTEE }, 337 { "fwd", TOK_FORWARD }, 338 { "forward", TOK_FORWARD }, 339 { "skipto", TOK_SKIPTO }, 340 { "deny", TOK_DENY }, 341 { "drop", TOK_DENY }, 342 { "reject", TOK_REJECT }, 343 { "reset6", TOK_RESET6 }, 344 { "reset", TOK_RESET }, 345 { "unreach6", TOK_UNREACH6 }, 346 { "unreach", TOK_UNREACH }, 347 { "check-state", TOK_CHECKSTATE }, 348 { "//", TOK_COMMENT }, 349 { NULL, 0 } /* terminator */ 350}; 351 352struct _s_x rule_action_params[] = { 353 { "altq", TOK_ALTQ }, 354 { "log", TOK_LOG }, 355 { "tag", TOK_TAG }, 356 { "untag", TOK_UNTAG }, 357 { NULL, 0 } /* terminator */ 358}; 359 360struct _s_x rule_options[] = { 361 { "tagged", TOK_TAGGED }, 362 { "uid", TOK_UID }, 363 { "gid", TOK_GID }, 364 { "jail", TOK_JAIL }, 365 { "in", TOK_IN }, 366 { "limit", TOK_LIMIT }, 367 { "keep-state", TOK_KEEPSTATE }, 368 { "bridged", TOK_LAYER2 }, 369 { "layer2", TOK_LAYER2 }, 370 { "out", TOK_OUT }, 371 { "diverted", TOK_DIVERTED }, 372 { "diverted-loopback", TOK_DIVERTEDLOOPBACK }, 373 { "diverted-output", TOK_DIVERTEDOUTPUT }, 374 { "xmit", TOK_XMIT }, 375 { "recv", TOK_RECV }, 376 { "via", TOK_VIA }, 377 { "fragment", TOK_FRAG }, 378 { "frag", TOK_FRAG }, 379 { "ipoptions", TOK_IPOPTS }, 380 { "ipopts", TOK_IPOPTS }, 381 { "iplen", TOK_IPLEN }, 382 { "ipid", TOK_IPID }, 383 { "ipprecedence", TOK_IPPRECEDENCE }, 384 { "iptos", TOK_IPTOS }, 385 { "ipttl", TOK_IPTTL }, 386 { "ipversion", TOK_IPVER }, 387 { "ipver", TOK_IPVER }, 388 { "estab", TOK_ESTAB }, 389 { "established", TOK_ESTAB }, 390 { "setup", TOK_SETUP }, 391 { "tcpdatalen", TOK_TCPDATALEN }, 392 { "tcpflags", TOK_TCPFLAGS }, 393 { "tcpflgs", TOK_TCPFLAGS }, 394 { "tcpoptions", TOK_TCPOPTS }, 395 { "tcpopts", TOK_TCPOPTS }, 396 { "tcpseq", TOK_TCPSEQ }, 397 { "tcpack", TOK_TCPACK }, 398 { "tcpwin", TOK_TCPWIN }, 399 { "icmptype", TOK_ICMPTYPES }, 400 { "icmptypes", TOK_ICMPTYPES }, 401 { "dst-ip", TOK_DSTIP }, 402 { "src-ip", TOK_SRCIP }, 403 { "dst-port", TOK_DSTPORT }, 404 { "src-port", TOK_SRCPORT }, 405 { "proto", TOK_PROTO }, 406 { "MAC", TOK_MAC }, 407 { "mac", TOK_MAC }, 408 { "mac-type", TOK_MACTYPE }, 409 { "verrevpath", TOK_VERREVPATH }, 410 { "versrcreach", TOK_VERSRCREACH }, 411 { "antispoof", TOK_ANTISPOOF }, 412 { "ipsec", TOK_IPSEC }, 413 { "icmp6type", TOK_ICMP6TYPES }, 414 { "icmp6types", TOK_ICMP6TYPES }, 415 { "ext6hdr", TOK_EXT6HDR}, 416 { "flow-id", TOK_FLOWID}, 417 { "ipv6", TOK_IPV6}, 418 { "ip6", TOK_IPV6}, 419 { "ipv4", TOK_IPV4}, 420 { "ip4", TOK_IPV4}, 421 { "dst-ipv6", TOK_DSTIP6}, 422 { "dst-ip6", TOK_DSTIP6}, 423 { "src-ipv6", TOK_SRCIP6}, 424 { "src-ip6", TOK_SRCIP6}, 425 { "//", TOK_COMMENT }, 426 427 { "not", TOK_NOT }, /* pseudo option */ 428 { "!", /* escape ? */ TOK_NOT }, /* pseudo option */ 429 { "or", TOK_OR }, /* pseudo option */ 430 { "|", /* escape */ TOK_OR }, /* pseudo option */ 431 { "{", TOK_STARTBRACE }, /* pseudo option */ 432 { "(", TOK_STARTBRACE }, /* pseudo option */ 433 { "}", TOK_ENDBRACE }, /* pseudo option */ 434 { ")", TOK_ENDBRACE }, /* pseudo option */ 435 { NULL, 0 } /* terminator */ 436}; 437 438#define TABLEARG "tablearg" 439 440static __inline uint64_t 441align_uint64(uint64_t *pll) { 442 uint64_t ret; 443 444 bcopy (pll, &ret, sizeof(ret)); 445 return ret; 446} 447 448/* 449 * conditionally runs the command. 450 */ 451static int 452do_cmd(int optname, void *optval, uintptr_t optlen) 453{ 454 static int s = -1; /* the socket */ 455 int i; 456 457 if (test_only) 458 return 0; 459 460 if (s == -1) 461 s = socket(AF_INET, SOCK_RAW, IPPROTO_RAW); 462 if (s < 0) 463 err(EX_UNAVAILABLE, "socket"); 464 465 if (optname == IP_FW_GET || optname == IP_DUMMYNET_GET || 466 optname == IP_FW_ADD || optname == IP_FW_TABLE_LIST || 467 optname == IP_FW_TABLE_GETSIZE) 468 i = getsockopt(s, IPPROTO_IP, optname, optval, 469 (socklen_t *)optlen); 470 else 471 i = setsockopt(s, IPPROTO_IP, optname, optval, optlen); 472 return i; 473} 474 475/** 476 * match_token takes a table and a string, returns the value associated 477 * with the string (-1 in case of failure). 478 */ 479static int 480match_token(struct _s_x *table, char *string) 481{ 482 struct _s_x *pt; 483 uint i = strlen(string); 484 485 for (pt = table ; i && pt->s != NULL ; pt++) 486 if (strlen(pt->s) == i && !bcmp(string, pt->s, i)) 487 return pt->x; 488 return -1; 489} 490 491/** 492 * match_value takes a table and a value, returns the string associated 493 * with the value (NULL in case of failure). 494 */ 495static char const * 496match_value(struct _s_x *p, int value) 497{ 498 for (; p->s != NULL; p++) 499 if (p->x == value) 500 return p->s; 501 return NULL; 502} 503 504/* 505 * _substrcmp takes two strings and returns 1 if they do not match, 506 * and 0 if they match exactly or the first string is a sub-string 507 * of the second. A warning is printed to stderr in the case that the 508 * first string is a sub-string of the second. 509 * 510 * This function will be removed in the future through the usual 511 * deprecation process. 512 */ 513static int 514_substrcmp(const char *str1, const char* str2) 515{ 516 517 if (strncmp(str1, str2, strlen(str1)) != 0) 518 return 1; 519 520 if (strlen(str1) != strlen(str2)) 521 warnx("DEPRECATED: '%s' matched '%s' as a sub-string", 522 str1, str2); 523 return 0; 524} 525 526/* 527 * _substrcmp2 takes three strings and returns 1 if the first two do not match, 528 * and 0 if they match exactly or the second string is a sub-string 529 * of the first. A warning is printed to stderr in the case that the 530 * first string does not match the third. 531 * 532 * This function exists to warn about the bizzare construction 533 * strncmp(str, "by", 2) which is used to allow people to use a shotcut 534 * for "bytes". The problem is that in addition to accepting "by", 535 * "byt", "byte", and "bytes", it also excepts "by_rabid_dogs" and any 536 * other string beginning with "by". 537 * 538 * This function will be removed in the future through the usual 539 * deprecation process. 540 */ 541static int 542_substrcmp2(const char *str1, const char* str2, const char* str3) 543{ 544 545 if (strncmp(str1, str2, strlen(str2)) != 0) 546 return 1; 547 548 if (strcmp(str1, str3) != 0) 549 warnx("DEPRECATED: '%s' matched '%s'", 550 str1, str3); 551 return 0; 552} 553 554/* 555 * prints one port, symbolic or numeric 556 */ 557static void 558print_port(int proto, uint16_t port) 559{ 560 561 if (proto == IPPROTO_ETHERTYPE) { 562 char const *s; 563 564 if (do_resolv && (s = match_value(ether_types, port)) ) 565 printf("%s", s); 566 else 567 printf("0x%04x", port); 568 } else { 569 struct servent *se = NULL; 570 if (do_resolv) { 571 struct protoent *pe = getprotobynumber(proto); 572 573 se = getservbyport(htons(port), pe ? pe->p_name : NULL); 574 } 575 if (se) 576 printf("%s", se->s_name); 577 else 578 printf("%d", port); 579 } 580} 581 582struct _s_x _port_name[] = { 583 {"dst-port", O_IP_DSTPORT}, 584 {"src-port", O_IP_SRCPORT}, 585 {"ipid", O_IPID}, 586 {"iplen", O_IPLEN}, 587 {"ipttl", O_IPTTL}, 588 {"mac-type", O_MAC_TYPE}, 589 {"tcpdatalen", O_TCPDATALEN}, 590 {"tagged", O_TAGGED}, 591 {NULL, 0} 592}; 593 594/* 595 * Print the values in a list 16-bit items of the types above. 596 * XXX todo: add support for mask. 597 */ 598static void 599print_newports(ipfw_insn_u16 *cmd, int proto, int opcode) 600{ 601 uint16_t *p = cmd->ports; 602 int i; 603 char const *sep; 604 605 if (cmd->o.len & F_NOT) 606 printf(" not"); 607 if (opcode != 0) { 608 sep = match_value(_port_name, opcode); 609 if (sep == NULL) 610 sep = "???"; 611 printf (" %s", sep); 612 } 613 sep = " "; 614 for (i = F_LEN((ipfw_insn *)cmd) - 1; i > 0; i--, p += 2) { 615 printf(sep); 616 print_port(proto, p[0]); 617 if (p[0] != p[1]) { 618 printf("-"); 619 print_port(proto, p[1]); 620 } 621 sep = ","; 622 } 623} 624 625/* 626 * Like strtol, but also translates service names into port numbers 627 * for some protocols. 628 * In particular: 629 * proto == -1 disables the protocol check; 630 * proto == IPPROTO_ETHERTYPE looks up an internal table 631 * proto == <some value in /etc/protocols> matches the values there. 632 * Returns *end == s in case the parameter is not found. 633 */ 634static int 635strtoport(char *s, char **end, int base, int proto) 636{ 637 char *p, *buf; 638 char *s1; 639 int i; 640 641 *end = s; /* default - not found */ 642 if (*s == '\0') 643 return 0; /* not found */ 644 645 if (isdigit(*s)) 646 return strtol(s, end, base); 647 648 /* 649 * find separator. '\\' escapes the next char. 650 */ 651 for (s1 = s; *s1 && (isalnum(*s1) || *s1 == '\\') ; s1++) 652 if (*s1 == '\\' && s1[1] != '\0') 653 s1++; 654 655 buf = malloc(s1 - s + 1); 656 if (buf == NULL) 657 return 0; 658 659 /* 660 * copy into a buffer skipping backslashes 661 */ 662 for (p = s, i = 0; p != s1 ; p++) 663 if (*p != '\\') 664 buf[i++] = *p; 665 buf[i++] = '\0'; 666 667 if (proto == IPPROTO_ETHERTYPE) { 668 i = match_token(ether_types, buf); 669 free(buf); 670 if (i != -1) { /* found */ 671 *end = s1; 672 return i; 673 } 674 } else { 675 struct protoent *pe = NULL; 676 struct servent *se; 677 678 if (proto != 0) 679 pe = getprotobynumber(proto); 680 setservent(1); 681 se = getservbyname(buf, pe ? pe->p_name : NULL); 682 free(buf); 683 if (se != NULL) { 684 *end = s1; 685 return ntohs(se->s_port); 686 } 687 } 688 return 0; /* not found */ 689} 690 691/* 692 * Map between current altq queue id numbers and names. 693 */ 694static int altq_fetched = 0; 695static TAILQ_HEAD(, pf_altq) altq_entries = 696 TAILQ_HEAD_INITIALIZER(altq_entries); 697 698static void 699altq_set_enabled(int enabled) 700{ 701 int pffd; 702 703 pffd = open("/dev/pf", O_RDWR); 704 if (pffd == -1) 705 err(EX_UNAVAILABLE, 706 "altq support opening pf(4) control device"); 707 if (enabled) { 708 if (ioctl(pffd, DIOCSTARTALTQ) != 0 && errno != EEXIST) 709 err(EX_UNAVAILABLE, "enabling altq"); 710 } else { 711 if (ioctl(pffd, DIOCSTOPALTQ) != 0 && errno != ENOENT) 712 err(EX_UNAVAILABLE, "disabling altq"); 713 } 714 close(pffd); 715} 716 717static void 718altq_fetch() 719{ 720 struct pfioc_altq pfioc; 721 struct pf_altq *altq; 722 int pffd, mnr; 723 724 if (altq_fetched) 725 return; 726 altq_fetched = 1; 727 pffd = open("/dev/pf", O_RDONLY); 728 if (pffd == -1) { 729 warn("altq support opening pf(4) control device"); 730 return; 731 } 732 bzero(&pfioc, sizeof(pfioc)); 733 if (ioctl(pffd, DIOCGETALTQS, &pfioc) != 0) { 734 warn("altq support getting queue list"); 735 close(pffd); 736 return; 737 } 738 mnr = pfioc.nr; 739 for (pfioc.nr = 0; pfioc.nr < mnr; pfioc.nr++) { 740 if (ioctl(pffd, DIOCGETALTQ, &pfioc) != 0) { 741 if (errno == EBUSY) 742 break; 743 warn("altq support getting queue list"); 744 close(pffd); 745 return; 746 } 747 if (pfioc.altq.qid == 0) 748 continue; 749 altq = malloc(sizeof(*altq)); 750 if (altq == NULL) 751 err(EX_OSERR, "malloc"); 752 *altq = pfioc.altq; 753 TAILQ_INSERT_TAIL(&altq_entries, altq, entries); 754 } 755 close(pffd); 756} 757 758static u_int32_t 759altq_name_to_qid(const char *name) 760{ 761 struct pf_altq *altq; 762 763 altq_fetch(); 764 TAILQ_FOREACH(altq, &altq_entries, entries) 765 if (strcmp(name, altq->qname) == 0) 766 break; 767 if (altq == NULL) 768 errx(EX_DATAERR, "altq has no queue named `%s'", name); 769 return altq->qid; 770} 771 772static const char * 773altq_qid_to_name(u_int32_t qid) 774{ 775 struct pf_altq *altq; 776 777 altq_fetch(); 778 TAILQ_FOREACH(altq, &altq_entries, entries) 779 if (qid == altq->qid) 780 break; 781 if (altq == NULL) 782 return NULL; 783 return altq->qname; 784} 785 786static void 787fill_altq_qid(u_int32_t *qid, const char *av) 788{ 789 *qid = altq_name_to_qid(av); 790} 791 792/* 793 * Fill the body of the command with the list of port ranges. 794 */ 795static int 796fill_newports(ipfw_insn_u16 *cmd, char *av, int proto) 797{ 798 uint16_t a, b, *p = cmd->ports; 799 int i = 0; 800 char *s = av; 801 802 while (*s) { 803 a = strtoport(av, &s, 0, proto); 804 if (s == av) /* no parameter */ 805 break; 806 if (*s == '-') { /* a range */ 807 av = s+1; 808 b = strtoport(av, &s, 0, proto); 809 if (s == av) /* no parameter */ 810 break; 811 p[0] = a; 812 p[1] = b; 813 } else if (*s == ',' || *s == '\0' ) 814 p[0] = p[1] = a; 815 else /* invalid separator */ 816 errx(EX_DATAERR, "invalid separator <%c> in <%s>\n", 817 *s, av); 818 i++; 819 p += 2; 820 av = s+1; 821 } 822 if (i > 0) { 823 if (i+1 > F_LEN_MASK) 824 errx(EX_DATAERR, "too many ports/ranges\n"); 825 cmd->o.len |= i+1; /* leave F_NOT and F_OR untouched */ 826 } 827 return i; 828} 829 830static struct _s_x icmpcodes[] = { 831 { "net", ICMP_UNREACH_NET }, 832 { "host", ICMP_UNREACH_HOST }, 833 { "protocol", ICMP_UNREACH_PROTOCOL }, 834 { "port", ICMP_UNREACH_PORT }, 835 { "needfrag", ICMP_UNREACH_NEEDFRAG }, 836 { "srcfail", ICMP_UNREACH_SRCFAIL }, 837 { "net-unknown", ICMP_UNREACH_NET_UNKNOWN }, 838 { "host-unknown", ICMP_UNREACH_HOST_UNKNOWN }, 839 { "isolated", ICMP_UNREACH_ISOLATED }, 840 { "net-prohib", ICMP_UNREACH_NET_PROHIB }, 841 { "host-prohib", ICMP_UNREACH_HOST_PROHIB }, 842 { "tosnet", ICMP_UNREACH_TOSNET }, 843 { "toshost", ICMP_UNREACH_TOSHOST }, 844 { "filter-prohib", ICMP_UNREACH_FILTER_PROHIB }, 845 { "host-precedence", ICMP_UNREACH_HOST_PRECEDENCE }, 846 { "precedence-cutoff", ICMP_UNREACH_PRECEDENCE_CUTOFF }, 847 { NULL, 0 } 848}; 849 850static void 851fill_reject_code(u_short *codep, char *str) 852{ 853 int val; 854 char *s; 855 856 val = strtoul(str, &s, 0); 857 if (s == str || *s != '\0' || val >= 0x100) 858 val = match_token(icmpcodes, str); 859 if (val < 0) 860 errx(EX_DATAERR, "unknown ICMP unreachable code ``%s''", str); 861 *codep = val; 862 return; 863} 864 865static void 866print_reject_code(uint16_t code) 867{ 868 char const *s = match_value(icmpcodes, code); 869 870 if (s != NULL) 871 printf("unreach %s", s); 872 else 873 printf("unreach %u", code); 874} 875 876static struct _s_x icmp6codes[] = { 877 { "no-route", ICMP6_DST_UNREACH_NOROUTE }, 878 { "admin-prohib", ICMP6_DST_UNREACH_ADMIN }, 879 { "address", ICMP6_DST_UNREACH_ADDR }, 880 { "port", ICMP6_DST_UNREACH_NOPORT }, 881 { NULL, 0 } 882}; 883 884static void 885fill_unreach6_code(u_short *codep, char *str) 886{ 887 int val; 888 char *s; 889 890 val = strtoul(str, &s, 0); 891 if (s == str || *s != '\0' || val >= 0x100) 892 val = match_token(icmp6codes, str); 893 if (val < 0) 894 errx(EX_DATAERR, "unknown ICMPv6 unreachable code ``%s''", str); 895 *codep = val; 896 return; 897} 898 899static void 900print_unreach6_code(uint16_t code) 901{ 902 char const *s = match_value(icmp6codes, code); 903 904 if (s != NULL) 905 printf("unreach6 %s", s); 906 else 907 printf("unreach6 %u", code); 908} 909 910/* 911 * Returns the number of bits set (from left) in a contiguous bitmask, 912 * or -1 if the mask is not contiguous. 913 * XXX this needs a proper fix. 914 * This effectively works on masks in big-endian (network) format. 915 * when compiled on little endian architectures. 916 * 917 * First bit is bit 7 of the first byte -- note, for MAC addresses, 918 * the first bit on the wire is bit 0 of the first byte. 919 * len is the max length in bits. 920 */ 921static int 922contigmask(uint8_t *p, int len) 923{ 924 int i, n; 925 926 for (i=0; i<len ; i++) 927 if ( (p[i/8] & (1 << (7 - (i%8)))) == 0) /* first bit unset */ 928 break; 929 for (n=i+1; n < len; n++) 930 if ( (p[n/8] & (1 << (7 - (n%8)))) != 0) 931 return -1; /* mask not contiguous */ 932 return i; 933} 934 935/* 936 * print flags set/clear in the two bitmasks passed as parameters. 937 * There is a specialized check for f_tcpflags. 938 */ 939static void 940print_flags(char const *name, ipfw_insn *cmd, struct _s_x *list) 941{ 942 char const *comma = ""; 943 int i; 944 uint8_t set = cmd->arg1 & 0xff; 945 uint8_t clear = (cmd->arg1 >> 8) & 0xff; 946 947 if (list == f_tcpflags && set == TH_SYN && clear == TH_ACK) { 948 printf(" setup"); 949 return; 950 } 951 952 printf(" %s ", name); 953 for (i=0; list[i].x != 0; i++) { 954 if (set & list[i].x) { 955 set &= ~list[i].x; 956 printf("%s%s", comma, list[i].s); 957 comma = ","; 958 } 959 if (clear & list[i].x) { 960 clear &= ~list[i].x; 961 printf("%s!%s", comma, list[i].s); 962 comma = ","; 963 } 964 } 965} 966 967/* 968 * Print the ip address contained in a command. 969 */ 970static void 971print_ip(ipfw_insn_ip *cmd, char const *s) 972{ 973 struct hostent *he = NULL; 974 int len = F_LEN((ipfw_insn *)cmd); 975 uint32_t *a = ((ipfw_insn_u32 *)cmd)->d; 976 977 printf("%s%s ", cmd->o.len & F_NOT ? " not": "", s); 978 979 if (cmd->o.opcode == O_IP_SRC_ME || cmd->o.opcode == O_IP_DST_ME) { 980 printf("me"); 981 return; 982 } 983 if (cmd->o.opcode == O_IP_SRC_LOOKUP || 984 cmd->o.opcode == O_IP_DST_LOOKUP) { 985 printf("table(%u", ((ipfw_insn *)cmd)->arg1); 986 if (len == F_INSN_SIZE(ipfw_insn_u32)) 987 printf(",%u", *a); 988 printf(")"); 989 return; 990 } 991 if (cmd->o.opcode == O_IP_SRC_SET || cmd->o.opcode == O_IP_DST_SET) { 992 uint32_t x, *map = (uint32_t *)&(cmd->mask); 993 int i, j; 994 char comma = '{'; 995 996 x = cmd->o.arg1 - 1; 997 x = htonl( ~x ); 998 cmd->addr.s_addr = htonl(cmd->addr.s_addr); 999 printf("%s/%d", inet_ntoa(cmd->addr), 1000 contigmask((uint8_t *)&x, 32)); 1001 x = cmd->addr.s_addr = htonl(cmd->addr.s_addr); 1002 x &= 0xff; /* base */ 1003 /* 1004 * Print bits and ranges. 1005 * Locate first bit set (i), then locate first bit unset (j). 1006 * If we have 3+ consecutive bits set, then print them as a 1007 * range, otherwise only print the initial bit and rescan. 1008 */ 1009 for (i=0; i < cmd->o.arg1; i++) 1010 if (map[i/32] & (1<<(i & 31))) { 1011 for (j=i+1; j < cmd->o.arg1; j++) 1012 if (!(map[ j/32] & (1<<(j & 31)))) 1013 break; 1014 printf("%c%d", comma, i+x); 1015 if (j>i+2) { /* range has at least 3 elements */ 1016 printf("-%d", j-1+x); 1017 i = j-1; 1018 } 1019 comma = ','; 1020 } 1021 printf("}"); 1022 return; 1023 } 1024 /* 1025 * len == 2 indicates a single IP, whereas lists of 1 or more 1026 * addr/mask pairs have len = (2n+1). We convert len to n so we 1027 * use that to count the number of entries. 1028 */ 1029 for (len = len / 2; len > 0; len--, a += 2) { 1030 int mb = /* mask length */ 1031 (cmd->o.opcode == O_IP_SRC || cmd->o.opcode == O_IP_DST) ? 1032 32 : contigmask((uint8_t *)&(a[1]), 32); 1033 if (mb == 32 && do_resolv) 1034 he = gethostbyaddr((char *)&(a[0]), sizeof(u_long), AF_INET); 1035 if (he != NULL) /* resolved to name */ 1036 printf("%s", he->h_name); 1037 else if (mb == 0) /* any */ 1038 printf("any"); 1039 else { /* numeric IP followed by some kind of mask */ 1040 printf("%s", inet_ntoa( *((struct in_addr *)&a[0]) ) ); 1041 if (mb < 0) 1042 printf(":%s", inet_ntoa( *((struct in_addr *)&a[1]) ) ); 1043 else if (mb < 32) 1044 printf("/%d", mb); 1045 } 1046 if (len > 1) 1047 printf(","); 1048 } 1049} 1050 1051/* 1052 * prints a MAC address/mask pair 1053 */ 1054static void 1055print_mac(uint8_t *addr, uint8_t *mask) 1056{ 1057 int l = contigmask(mask, 48); 1058 1059 if (l == 0) 1060 printf(" any"); 1061 else { 1062 printf(" %02x:%02x:%02x:%02x:%02x:%02x", 1063 addr[0], addr[1], addr[2], addr[3], addr[4], addr[5]); 1064 if (l == -1) 1065 printf("&%02x:%02x:%02x:%02x:%02x:%02x", 1066 mask[0], mask[1], mask[2], 1067 mask[3], mask[4], mask[5]); 1068 else if (l < 48) 1069 printf("/%d", l); 1070 } 1071} 1072 1073static void 1074fill_icmptypes(ipfw_insn_u32 *cmd, char *av) 1075{ 1076 uint8_t type; 1077 1078 cmd->d[0] = 0; 1079 while (*av) { 1080 if (*av == ',') 1081 av++; 1082 1083 type = strtoul(av, &av, 0); 1084 1085 if (*av != ',' && *av != '\0') 1086 errx(EX_DATAERR, "invalid ICMP type"); 1087 1088 if (type > 31) 1089 errx(EX_DATAERR, "ICMP type out of range"); 1090 1091 cmd->d[0] |= 1 << type; 1092 } 1093 cmd->o.opcode = O_ICMPTYPE; 1094 cmd->o.len |= F_INSN_SIZE(ipfw_insn_u32); 1095} 1096 1097static void 1098print_icmptypes(ipfw_insn_u32 *cmd) 1099{ 1100 int i; 1101 char sep= ' '; 1102 1103 printf(" icmptypes"); 1104 for (i = 0; i < 32; i++) { 1105 if ( (cmd->d[0] & (1 << (i))) == 0) 1106 continue; 1107 printf("%c%d", sep, i); 1108 sep = ','; 1109 } 1110} 1111 1112/* 1113 * Print the ip address contained in a command. 1114 */ 1115static void 1116print_ip6(ipfw_insn_ip6 *cmd, char const *s) 1117{ 1118 struct hostent *he = NULL; 1119 int len = F_LEN((ipfw_insn *) cmd) - 1; 1120 struct in6_addr *a = &(cmd->addr6); 1121 char trad[255]; 1122 1123 printf("%s%s ", cmd->o.len & F_NOT ? " not": "", s); 1124 1125 if (cmd->o.opcode == O_IP6_SRC_ME || cmd->o.opcode == O_IP6_DST_ME) { 1126 printf("me6"); 1127 return; 1128 } 1129 if (cmd->o.opcode == O_IP6) { 1130 printf(" ip6"); 1131 return; 1132 } 1133 1134 /* 1135 * len == 4 indicates a single IP, whereas lists of 1 or more 1136 * addr/mask pairs have len = (2n+1). We convert len to n so we 1137 * use that to count the number of entries. 1138 */ 1139 1140 for (len = len / 4; len > 0; len -= 2, a += 2) { 1141 int mb = /* mask length */ 1142 (cmd->o.opcode == O_IP6_SRC || cmd->o.opcode == O_IP6_DST) ? 1143 128 : contigmask((uint8_t *)&(a[1]), 128); 1144 1145 if (mb == 128 && do_resolv) 1146 he = gethostbyaddr((char *)a, sizeof(*a), AF_INET6); 1147 if (he != NULL) /* resolved to name */ 1148 printf("%s", he->h_name); 1149 else if (mb == 0) /* any */ 1150 printf("any"); 1151 else { /* numeric IP followed by some kind of mask */ 1152 if (inet_ntop(AF_INET6, a, trad, sizeof( trad ) ) == NULL) 1153 printf("Error ntop in print_ip6\n"); 1154 printf("%s", trad ); 1155 if (mb < 0) /* XXX not really legal... */ 1156 printf(":%s", 1157 inet_ntop(AF_INET6, &a[1], trad, sizeof(trad))); 1158 else if (mb < 128) 1159 printf("/%d", mb); 1160 } 1161 if (len > 2) 1162 printf(","); 1163 } 1164} 1165 1166static void 1167fill_icmp6types(ipfw_insn_icmp6 *cmd, char *av) 1168{ 1169 uint8_t type; 1170 1171 cmd->d[0] = 0; 1172 while (*av) { 1173 if (*av == ',') 1174 av++; 1175 type = strtoul(av, &av, 0); 1176 if (*av != ',' && *av != '\0') 1177 errx(EX_DATAERR, "invalid ICMP6 type"); 1178 /* 1179 * XXX: shouldn't this be 0xFF? I can't see any reason why 1180 * we shouldn't be able to filter all possiable values 1181 * regardless of the ability of the rest of the kernel to do 1182 * anything useful with them. 1183 */ 1184 if (type > ICMP6_MAXTYPE) 1185 errx(EX_DATAERR, "ICMP6 type out of range"); 1186 cmd->d[type / 32] |= ( 1 << (type % 32)); 1187 } 1188 cmd->o.opcode = O_ICMP6TYPE; 1189 cmd->o.len |= F_INSN_SIZE(ipfw_insn_icmp6); 1190} 1191 1192 1193static void 1194print_icmp6types(ipfw_insn_u32 *cmd) 1195{ 1196 int i, j; 1197 char sep= ' '; 1198 1199 printf(" ip6 icmp6types"); 1200 for (i = 0; i < 7; i++) 1201 for (j=0; j < 32; ++j) { 1202 if ( (cmd->d[i] & (1 << (j))) == 0) 1203 continue; 1204 printf("%c%d", sep, (i*32 + j)); 1205 sep = ','; 1206 } 1207} 1208 1209static void 1210print_flow6id( ipfw_insn_u32 *cmd) 1211{ 1212 uint16_t i, limit = cmd->o.arg1; 1213 char sep = ','; 1214 1215 printf(" flow-id "); 1216 for( i=0; i < limit; ++i) { 1217 if (i == limit - 1) 1218 sep = ' '; 1219 printf("%d%c", cmd->d[i], sep); 1220 } 1221} 1222 1223/* structure and define for the extension header in ipv6 */ 1224static struct _s_x ext6hdrcodes[] = { 1225 { "frag", EXT_FRAGMENT }, 1226 { "hopopt", EXT_HOPOPTS }, 1227 { "route", EXT_ROUTING }, 1228 { "dstopt", EXT_DSTOPTS }, 1229 { "ah", EXT_AH }, 1230 { "esp", EXT_ESP }, 1231 { NULL, 0 } 1232}; 1233 1234/* fills command for the extension header filtering */ 1235int 1236fill_ext6hdr( ipfw_insn *cmd, char *av) 1237{ 1238 int tok; 1239 char *s = av; 1240 1241 cmd->arg1 = 0; 1242 1243 while(s) { 1244 av = strsep( &s, ",") ; 1245 tok = match_token(ext6hdrcodes, av); 1246 switch (tok) { 1247 case EXT_FRAGMENT: 1248 cmd->arg1 |= EXT_FRAGMENT; 1249 break; 1250 1251 case EXT_HOPOPTS: 1252 cmd->arg1 |= EXT_HOPOPTS; 1253 break; 1254 1255 case EXT_ROUTING: 1256 cmd->arg1 |= EXT_ROUTING; 1257 break; 1258 1259 case EXT_DSTOPTS: 1260 cmd->arg1 |= EXT_DSTOPTS; 1261 break; 1262 1263 case EXT_AH: 1264 cmd->arg1 |= EXT_AH; 1265 break; 1266 1267 case EXT_ESP: 1268 cmd->arg1 |= EXT_ESP; 1269 break; 1270 1271 default: 1272 errx( EX_DATAERR, "invalid option for ipv6 exten header" ); 1273 break; 1274 } 1275 } 1276 if (cmd->arg1 == 0 ) 1277 return 0; 1278 cmd->opcode = O_EXT_HDR; 1279 cmd->len |= F_INSN_SIZE( ipfw_insn ); 1280 return 1; 1281} 1282 1283void 1284print_ext6hdr( ipfw_insn *cmd ) 1285{ 1286 char sep = ' '; 1287 1288 printf(" extension header:"); 1289 if (cmd->arg1 & EXT_FRAGMENT ) { 1290 printf("%cfragmentation", sep); 1291 sep = ','; 1292 } 1293 if (cmd->arg1 & EXT_HOPOPTS ) { 1294 printf("%chop options", sep); 1295 sep = ','; 1296 } 1297 if (cmd->arg1 & EXT_ROUTING ) { 1298 printf("%crouting options", sep); 1299 sep = ','; 1300 } 1301 if (cmd->arg1 & EXT_DSTOPTS ) { 1302 printf("%cdestination options", sep); 1303 sep = ','; 1304 } 1305 if (cmd->arg1 & EXT_AH ) { 1306 printf("%cauthentication header", sep); 1307 sep = ','; 1308 } 1309 if (cmd->arg1 & EXT_ESP ) { 1310 printf("%cencapsulated security payload", sep); 1311 } 1312} 1313 1314/* 1315 * show_ipfw() prints the body of an ipfw rule. 1316 * Because the standard rule has at least proto src_ip dst_ip, we use 1317 * a helper function to produce these entries if not provided explicitly. 1318 * The first argument is the list of fields we have, the second is 1319 * the list of fields we want to be printed. 1320 * 1321 * Special cases if we have provided a MAC header: 1322 * + if the rule does not contain IP addresses/ports, do not print them; 1323 * + if the rule does not contain an IP proto, print "all" instead of "ip"; 1324 * 1325 * Once we have 'have_options', IP header fields are printed as options. 1326 */ 1327#define HAVE_PROTO 0x0001 1328#define HAVE_SRCIP 0x0002 1329#define HAVE_DSTIP 0x0004 1330#define HAVE_MAC 0x0008 1331#define HAVE_MACTYPE 0x0010 1332#define HAVE_PROTO4 0x0040 1333#define HAVE_PROTO6 0x0080 1334#define HAVE_OPTIONS 0x8000 1335 1336#define HAVE_IP (HAVE_PROTO | HAVE_SRCIP | HAVE_DSTIP) 1337static void 1338show_prerequisites(int *flags, int want, int cmd) 1339{ 1340 if (comment_only) 1341 return; 1342 if ( (*flags & HAVE_IP) == HAVE_IP) 1343 *flags |= HAVE_OPTIONS; 1344 1345 if ( (*flags & (HAVE_MAC|HAVE_MACTYPE|HAVE_OPTIONS)) == HAVE_MAC && 1346 cmd != O_MAC_TYPE) { 1347 /* 1348 * mac-type was optimized out by the compiler, 1349 * restore it 1350 */ 1351 printf(" any"); 1352 *flags |= HAVE_MACTYPE | HAVE_OPTIONS; 1353 return; 1354 } 1355 if ( !(*flags & HAVE_OPTIONS)) { 1356 if ( !(*flags & HAVE_PROTO) && (want & HAVE_PROTO)) 1357 if ( (*flags & HAVE_PROTO4)) 1358 printf(" ip4"); 1359 else if ( (*flags & HAVE_PROTO6)) 1360 printf(" ip6"); 1361 else 1362 printf(" ip"); 1363 1364 if ( !(*flags & HAVE_SRCIP) && (want & HAVE_SRCIP)) 1365 printf(" from any"); 1366 if ( !(*flags & HAVE_DSTIP) && (want & HAVE_DSTIP)) 1367 printf(" to any"); 1368 } 1369 *flags |= want; 1370} 1371 1372static void 1373show_ipfw(struct ip_fw *rule, int pcwidth, int bcwidth) 1374{ 1375 static int twidth = 0; 1376 int l; 1377 ipfw_insn *cmd, *tagptr = NULL; 1378 char *comment = NULL; /* ptr to comment if we have one */ 1379 int proto = 0; /* default */ 1380 int flags = 0; /* prerequisites */ 1381 ipfw_insn_log *logptr = NULL; /* set if we find an O_LOG */ 1382 ipfw_insn_altq *altqptr = NULL; /* set if we find an O_ALTQ */ 1383 int or_block = 0; /* we are in an or block */ 1384 uint32_t set_disable; 1385 1386 bcopy(&rule->next_rule, &set_disable, sizeof(set_disable)); 1387 1388 if (set_disable & (1 << rule->set)) { /* disabled */ 1389 if (!show_sets) 1390 return; 1391 else 1392 printf("# DISABLED "); 1393 } 1394 printf("%05u ", rule->rulenum); 1395 1396 if (pcwidth>0 || bcwidth>0) 1397 printf("%*llu %*llu ", pcwidth, align_uint64(&rule->pcnt), 1398 bcwidth, align_uint64(&rule->bcnt)); 1399 1400 if (do_time == 2) 1401 printf("%10u ", rule->timestamp); 1402 else if (do_time == 1) { 1403 char timestr[30]; 1404 time_t t = (time_t)0; 1405 1406 if (twidth == 0) { 1407 strcpy(timestr, ctime(&t)); 1408 *strchr(timestr, '\n') = '\0'; 1409 twidth = strlen(timestr); 1410 } 1411 if (rule->timestamp) { 1412 t = _long_to_time(rule->timestamp); 1413 1414 strcpy(timestr, ctime(&t)); 1415 *strchr(timestr, '\n') = '\0'; 1416 printf("%s ", timestr); 1417 } else { 1418 printf("%*s", twidth, " "); 1419 } 1420 } 1421 1422 if (show_sets) 1423 printf("set %d ", rule->set); 1424 1425 /* 1426 * print the optional "match probability" 1427 */ 1428 if (rule->cmd_len > 0) { 1429 cmd = rule->cmd ; 1430 if (cmd->opcode == O_PROB) { 1431 ipfw_insn_u32 *p = (ipfw_insn_u32 *)cmd; 1432 double d = 1.0 * p->d[0]; 1433 1434 d = (d / 0x7fffffff); 1435 printf("prob %f ", d); 1436 } 1437 } 1438 1439 /* 1440 * first print actions 1441 */ 1442 for (l = rule->cmd_len - rule->act_ofs, cmd = ACTION_PTR(rule); 1443 l > 0 ; l -= F_LEN(cmd), cmd += F_LEN(cmd)) { 1444 switch(cmd->opcode) { 1445 case O_CHECK_STATE: 1446 printf("check-state"); 1447 flags = HAVE_IP; /* avoid printing anything else */ 1448 break; 1449 1450 case O_ACCEPT: 1451 printf("allow"); 1452 break; 1453 1454 case O_COUNT: 1455 printf("count"); 1456 break; 1457 1458 case O_DENY: 1459 printf("deny"); 1460 break; 1461 1462 case O_REJECT: 1463 if (cmd->arg1 == ICMP_REJECT_RST) 1464 printf("reset"); 1465 else if (cmd->arg1 == ICMP_UNREACH_HOST) 1466 printf("reject"); 1467 else 1468 print_reject_code(cmd->arg1); 1469 break; 1470 1471 case O_UNREACH6: 1472 if (cmd->arg1 == ICMP6_UNREACH_RST) 1473 printf("reset6"); 1474 else 1475 print_unreach6_code(cmd->arg1); 1476 break; 1477 1478#define PRINT_WITH_ARG(o) \ 1479 if (cmd->arg1 == IP_FW_TABLEARG) \ 1480 printf("%s tablearg", (o)); \ 1481 else \ 1482 printf("%s %u", (o), cmd->arg1); \ 1483 break; 1484 1485 case O_SKIPTO: 1486 PRINT_WITH_ARG("skipto"); 1487 case O_PIPE: 1488 PRINT_WITH_ARG("pipe"); 1489 case O_QUEUE: 1490 PRINT_WITH_ARG("queue"); 1491 case O_DIVERT: 1492 PRINT_WITH_ARG("divert"); 1493 case O_TEE: 1494 PRINT_WITH_ARG("tee"); 1495 case O_NETGRAPH: 1496 PRINT_WITH_ARG("netgraph"); 1497 case O_NGTEE: 1498 PRINT_WITH_ARG("ngtee"); 1499#undef PRINT_WITH_ARG 1500 1501 case O_FORWARD_IP: 1502 { 1503 ipfw_insn_sa *s = (ipfw_insn_sa *)cmd; 1504 1505 printf("fwd %s", inet_ntoa(s->sa.sin_addr)); 1506 if (s->sa.sin_port) 1507 printf(",%d", s->sa.sin_port); 1508 } 1509 break; 1510 1511 case O_LOG: /* O_LOG is printed last */ 1512 logptr = (ipfw_insn_log *)cmd; 1513 break; 1514 1515 case O_ALTQ: /* O_ALTQ is printed after O_LOG */ 1516 altqptr = (ipfw_insn_altq *)cmd; 1517 break; 1518 1519 case O_TAG: 1520 tagptr = cmd; 1521 break; 1522 1523 default: 1524 printf("** unrecognized action %d len %d ", 1525 cmd->opcode, cmd->len); 1526 } 1527 } 1528 if (logptr) { 1529 if (logptr->max_log > 0) 1530 printf(" log logamount %d", logptr->max_log); 1531 else 1532 printf(" log"); 1533 } 1534 if (altqptr) { 1535 const char *qname; 1536 1537 qname = altq_qid_to_name(altqptr->qid); 1538 if (qname == NULL) 1539 printf(" altq ?<%u>", altqptr->qid); 1540 else 1541 printf(" altq %s", qname); 1542 } 1543 if (tagptr) { 1544 if (tagptr->len & F_NOT) 1545 printf(" untag %hu", tagptr->arg1); 1546 else 1547 printf(" tag %hu", tagptr->arg1); 1548 } 1549 1550 /* 1551 * then print the body. 1552 */ 1553 for (l = rule->act_ofs, cmd = rule->cmd ; 1554 l > 0 ; l -= F_LEN(cmd) , cmd += F_LEN(cmd)) { 1555 if ((cmd->len & F_OR) || (cmd->len & F_NOT)) 1556 continue; 1557 if (cmd->opcode == O_IP4) { 1558 flags |= HAVE_PROTO4; 1559 break; 1560 } else if (cmd->opcode == O_IP6) { 1561 flags |= HAVE_PROTO6; 1562 break; 1563 } 1564 } 1565 if (rule->_pad & 1) { /* empty rules before options */ 1566 if (!do_compact) { 1567 show_prerequisites(&flags, HAVE_PROTO, 0); 1568 printf(" from any to any"); 1569 } 1570 flags |= HAVE_IP | HAVE_OPTIONS; 1571 } 1572 1573 if (comment_only) 1574 comment = "..."; 1575 1576 for (l = rule->act_ofs, cmd = rule->cmd ; 1577 l > 0 ; l -= F_LEN(cmd) , cmd += F_LEN(cmd)) { 1578 /* useful alias */ 1579 ipfw_insn_u32 *cmd32 = (ipfw_insn_u32 *)cmd; 1580 1581 if (comment_only) { 1582 if (cmd->opcode != O_NOP) 1583 continue; 1584 printf(" // %s\n", (char *)(cmd + 1)); 1585 return; 1586 } 1587 1588 show_prerequisites(&flags, 0, cmd->opcode); 1589 1590 switch(cmd->opcode) { 1591 case O_PROB: 1592 break; /* done already */ 1593 1594 case O_PROBE_STATE: 1595 break; /* no need to print anything here */ 1596 1597 case O_MACADDR2: { 1598 ipfw_insn_mac *m = (ipfw_insn_mac *)cmd; 1599 1600 if ((cmd->len & F_OR) && !or_block) 1601 printf(" {"); 1602 if (cmd->len & F_NOT) 1603 printf(" not"); 1604 printf(" MAC"); 1605 flags |= HAVE_MAC; 1606 print_mac(m->addr, m->mask); 1607 print_mac(m->addr + 6, m->mask + 6); 1608 } 1609 break; 1610 1611 case O_MAC_TYPE: 1612 if ((cmd->len & F_OR) && !or_block) 1613 printf(" {"); 1614 print_newports((ipfw_insn_u16 *)cmd, IPPROTO_ETHERTYPE, 1615 (flags & HAVE_OPTIONS) ? cmd->opcode : 0); 1616 flags |= HAVE_MAC | HAVE_MACTYPE | HAVE_OPTIONS; 1617 break; 1618 1619 case O_IP_SRC: 1620 case O_IP_SRC_LOOKUP: 1621 case O_IP_SRC_MASK: 1622 case O_IP_SRC_ME: 1623 case O_IP_SRC_SET: 1624 show_prerequisites(&flags, HAVE_PROTO, 0); 1625 if (!(flags & HAVE_SRCIP)) 1626 printf(" from"); 1627 if ((cmd->len & F_OR) && !or_block) 1628 printf(" {"); 1629 print_ip((ipfw_insn_ip *)cmd, 1630 (flags & HAVE_OPTIONS) ? " src-ip" : ""); 1631 flags |= HAVE_SRCIP; 1632 break; 1633 1634 case O_IP_DST: 1635 case O_IP_DST_LOOKUP: 1636 case O_IP_DST_MASK: 1637 case O_IP_DST_ME: 1638 case O_IP_DST_SET: 1639 show_prerequisites(&flags, HAVE_PROTO|HAVE_SRCIP, 0); 1640 if (!(flags & HAVE_DSTIP)) 1641 printf(" to"); 1642 if ((cmd->len & F_OR) && !or_block) 1643 printf(" {"); 1644 print_ip((ipfw_insn_ip *)cmd, 1645 (flags & HAVE_OPTIONS) ? " dst-ip" : ""); 1646 flags |= HAVE_DSTIP; 1647 break; 1648 1649 case O_IP6_SRC: 1650 case O_IP6_SRC_MASK: 1651 case O_IP6_SRC_ME: 1652 show_prerequisites(&flags, HAVE_PROTO, 0); 1653 if (!(flags & HAVE_SRCIP)) 1654 printf(" from"); 1655 if ((cmd->len & F_OR) && !or_block) 1656 printf(" {"); 1657 print_ip6((ipfw_insn_ip6 *)cmd, 1658 (flags & HAVE_OPTIONS) ? " src-ip6" : ""); 1659 flags |= HAVE_SRCIP | HAVE_PROTO; 1660 break; 1661 1662 case O_IP6_DST: 1663 case O_IP6_DST_MASK: 1664 case O_IP6_DST_ME: 1665 show_prerequisites(&flags, HAVE_PROTO|HAVE_SRCIP, 0); 1666 if (!(flags & HAVE_DSTIP)) 1667 printf(" to"); 1668 if ((cmd->len & F_OR) && !or_block) 1669 printf(" {"); 1670 print_ip6((ipfw_insn_ip6 *)cmd, 1671 (flags & HAVE_OPTIONS) ? " dst-ip6" : ""); 1672 flags |= HAVE_DSTIP; 1673 break; 1674 1675 case O_FLOW6ID: 1676 print_flow6id( (ipfw_insn_u32 *) cmd ); 1677 flags |= HAVE_OPTIONS; 1678 break; 1679 1680 case O_IP_DSTPORT: 1681 show_prerequisites(&flags, HAVE_IP, 0); 1682 case O_IP_SRCPORT: 1683 show_prerequisites(&flags, HAVE_PROTO|HAVE_SRCIP, 0); 1684 if ((cmd->len & F_OR) && !or_block) 1685 printf(" {"); 1686 print_newports((ipfw_insn_u16 *)cmd, proto, 1687 (flags & HAVE_OPTIONS) ? cmd->opcode : 0); 1688 break; 1689 1690 case O_PROTO: { 1691 struct protoent *pe = NULL; 1692 1693 if ((cmd->len & F_OR) && !or_block) 1694 printf(" {"); 1695 if (cmd->len & F_NOT) 1696 printf(" not"); 1697 proto = cmd->arg1; 1698 pe = getprotobynumber(cmd->arg1); 1699 if ((flags & (HAVE_PROTO4 | HAVE_PROTO6)) && 1700 !(flags & HAVE_PROTO)) 1701 show_prerequisites(&flags, 1702 HAVE_IP | HAVE_OPTIONS, 0); 1703 if (flags & HAVE_OPTIONS) 1704 printf(" proto"); 1705 if (pe) 1706 printf(" %s", pe->p_name); 1707 else 1708 printf(" %u", cmd->arg1); 1709 } 1710 flags |= HAVE_PROTO; 1711 break; 1712 1713 default: /*options ... */ 1714 if (!(cmd->len & (F_OR|F_NOT))) 1715 if (((cmd->opcode == O_IP6) && 1716 (flags & HAVE_PROTO6)) || 1717 ((cmd->opcode == O_IP4) && 1718 (flags & HAVE_PROTO4))) 1719 break; 1720 show_prerequisites(&flags, HAVE_IP | HAVE_OPTIONS, 0); 1721 if ((cmd->len & F_OR) && !or_block) 1722 printf(" {"); 1723 if (cmd->len & F_NOT && cmd->opcode != O_IN) 1724 printf(" not"); 1725 switch(cmd->opcode) { 1726 case O_FRAG: 1727 printf(" frag"); 1728 break; 1729 1730 case O_IN: 1731 printf(cmd->len & F_NOT ? " out" : " in"); 1732 break; 1733 1734 case O_DIVERTED: 1735 switch (cmd->arg1) { 1736 case 3: 1737 printf(" diverted"); 1738 break; 1739 case 1: 1740 printf(" diverted-loopback"); 1741 break; 1742 case 2: 1743 printf(" diverted-output"); 1744 break; 1745 default: 1746 printf(" diverted-?<%u>", cmd->arg1); 1747 break; 1748 } 1749 break; 1750 1751 case O_LAYER2: 1752 printf(" layer2"); 1753 break; 1754 case O_XMIT: 1755 case O_RECV: 1756 case O_VIA: 1757 { 1758 char const *s; 1759 ipfw_insn_if *cmdif = (ipfw_insn_if *)cmd; 1760 1761 if (cmd->opcode == O_XMIT) 1762 s = "xmit"; 1763 else if (cmd->opcode == O_RECV) 1764 s = "recv"; 1765 else /* if (cmd->opcode == O_VIA) */ 1766 s = "via"; 1767 if (cmdif->name[0] == '\0') 1768 printf(" %s %s", s, 1769 inet_ntoa(cmdif->p.ip)); 1770 else 1771 printf(" %s %s", s, cmdif->name); 1772 1773 break; 1774 } 1775 case O_IPID: 1776 if (F_LEN(cmd) == 1) 1777 printf(" ipid %u", cmd->arg1 ); 1778 else 1779 print_newports((ipfw_insn_u16 *)cmd, 0, 1780 O_IPID); 1781 break; 1782 1783 case O_IPTTL: 1784 if (F_LEN(cmd) == 1) 1785 printf(" ipttl %u", cmd->arg1 ); 1786 else 1787 print_newports((ipfw_insn_u16 *)cmd, 0, 1788 O_IPTTL); 1789 break; 1790 1791 case O_IPVER: 1792 printf(" ipver %u", cmd->arg1 ); 1793 break; 1794 1795 case O_IPPRECEDENCE: 1796 printf(" ipprecedence %u", (cmd->arg1) >> 5 ); 1797 break; 1798 1799 case O_IPLEN: 1800 if (F_LEN(cmd) == 1) 1801 printf(" iplen %u", cmd->arg1 ); 1802 else 1803 print_newports((ipfw_insn_u16 *)cmd, 0, 1804 O_IPLEN); 1805 break; 1806 1807 case O_IPOPT: 1808 print_flags("ipoptions", cmd, f_ipopts); 1809 break; 1810 1811 case O_IPTOS: 1812 print_flags("iptos", cmd, f_iptos); 1813 break; 1814 1815 case O_ICMPTYPE: 1816 print_icmptypes((ipfw_insn_u32 *)cmd); 1817 break; 1818 1819 case O_ESTAB: 1820 printf(" established"); 1821 break; 1822 1823 case O_TCPDATALEN: 1824 if (F_LEN(cmd) == 1) 1825 printf(" tcpdatalen %u", cmd->arg1 ); 1826 else 1827 print_newports((ipfw_insn_u16 *)cmd, 0, 1828 O_TCPDATALEN); 1829 break; 1830 1831 case O_TCPFLAGS: 1832 print_flags("tcpflags", cmd, f_tcpflags); 1833 break; 1834 1835 case O_TCPOPTS: 1836 print_flags("tcpoptions", cmd, f_tcpopts); 1837 break; 1838 1839 case O_TCPWIN: 1840 printf(" tcpwin %d", ntohs(cmd->arg1)); 1841 break; 1842 1843 case O_TCPACK: 1844 printf(" tcpack %d", ntohl(cmd32->d[0])); 1845 break; 1846 1847 case O_TCPSEQ: 1848 printf(" tcpseq %d", ntohl(cmd32->d[0])); 1849 break; 1850 1851 case O_UID: 1852 { 1853 struct passwd *pwd = getpwuid(cmd32->d[0]); 1854 1855 if (pwd) 1856 printf(" uid %s", pwd->pw_name); 1857 else 1858 printf(" uid %u", cmd32->d[0]); 1859 } 1860 break; 1861 1862 case O_GID: 1863 { 1864 struct group *grp = getgrgid(cmd32->d[0]); 1865 1866 if (grp) 1867 printf(" gid %s", grp->gr_name); 1868 else 1869 printf(" gid %u", cmd32->d[0]); 1870 } 1871 break; 1872 1873 case O_JAIL: 1874 printf(" jail %d", cmd32->d[0]); 1875 break; 1876 1877 case O_VERREVPATH: 1878 printf(" verrevpath"); 1879 break; 1880 1881 case O_VERSRCREACH: 1882 printf(" versrcreach"); 1883 break; 1884 1885 case O_ANTISPOOF: 1886 printf(" antispoof"); 1887 break; 1888 1889 case O_IPSEC: 1890 printf(" ipsec"); 1891 break; 1892 1893 case O_NOP: 1894 comment = (char *)(cmd + 1); 1895 break; 1896 1897 case O_KEEP_STATE: 1898 printf(" keep-state"); 1899 break; 1900 1901 case O_LIMIT: 1902 { 1903 struct _s_x *p = limit_masks; 1904 ipfw_insn_limit *c = (ipfw_insn_limit *)cmd; 1905 uint8_t x = c->limit_mask; 1906 char const *comma = " "; 1907 1908 printf(" limit"); 1909 for (; p->x != 0 ; p++) 1910 if ((x & p->x) == p->x) { 1911 x &= ~p->x; 1912 printf("%s%s", comma, p->s); 1913 comma = ","; 1914 } 1915 printf(" %d", c->conn_limit); 1916 } 1917 break; 1918 1919 case O_IP6: 1920 printf(" ip6"); 1921 break; 1922 1923 case O_IP4: 1924 printf(" ip4"); 1925 break; 1926 1927 case O_ICMP6TYPE: 1928 print_icmp6types((ipfw_insn_u32 *)cmd); 1929 break; 1930 1931 case O_EXT_HDR: 1932 print_ext6hdr( (ipfw_insn *) cmd ); 1933 break; 1934 1935 case O_TAGGED: 1936 if (F_LEN(cmd) == 1) 1937 printf(" tagged %hu", cmd->arg1 ); 1938 else 1939 print_newports((ipfw_insn_u16 *)cmd, 0, 1940 O_TAGGED); 1941 break; 1942 1943 default: 1944 printf(" [opcode %d len %d]", 1945 cmd->opcode, cmd->len); 1946 } 1947 } 1948 if (cmd->len & F_OR) { 1949 printf(" or"); 1950 or_block = 1; 1951 } else if (or_block) { 1952 printf(" }"); 1953 or_block = 0; 1954 } 1955 } 1956 show_prerequisites(&flags, HAVE_IP, 0); 1957 if (comment) 1958 printf(" // %s", comment); 1959 printf("\n"); 1960} 1961 1962static void 1963show_dyn_ipfw(ipfw_dyn_rule *d, int pcwidth, int bcwidth) 1964{ 1965 struct protoent *pe; 1966 struct in_addr a; 1967 uint16_t rulenum; 1968 1969 if (!do_expired) { 1970 if (!d->expire && !(d->dyn_type == O_LIMIT_PARENT)) 1971 return; 1972 } 1973 bcopy(&d->rule, &rulenum, sizeof(rulenum)); 1974 printf("%05d", rulenum); 1975 if (pcwidth>0 || bcwidth>0) 1976 printf(" %*llu %*llu (%ds)", pcwidth, 1977 align_uint64(&d->pcnt), bcwidth, 1978 align_uint64(&d->bcnt), d->expire); 1979 switch (d->dyn_type) { 1980 case O_LIMIT_PARENT: 1981 printf(" PARENT %d", d->count); 1982 break; 1983 case O_LIMIT: 1984 printf(" LIMIT"); 1985 break; 1986 case O_KEEP_STATE: /* bidir, no mask */ 1987 printf(" STATE"); 1988 break; 1989 } 1990 1991 if ((pe = getprotobynumber(d->id.proto)) != NULL) 1992 printf(" %s", pe->p_name); 1993 else 1994 printf(" proto %u", d->id.proto); 1995 1996 a.s_addr = htonl(d->id.src_ip); 1997 printf(" %s %d", inet_ntoa(a), d->id.src_port); 1998 1999 a.s_addr = htonl(d->id.dst_ip); 2000 printf(" <-> %s %d", inet_ntoa(a), d->id.dst_port); 2001 printf("\n"); 2002} 2003 2004static int 2005sort_q(const void *pa, const void *pb) 2006{ 2007 int rev = (do_sort < 0); 2008 int field = rev ? -do_sort : do_sort; 2009 long long res = 0; 2010 const struct dn_flow_queue *a = pa; 2011 const struct dn_flow_queue *b = pb; 2012 2013 switch (field) { 2014 case 1: /* pkts */ 2015 res = a->len - b->len; 2016 break; 2017 case 2: /* bytes */ 2018 res = a->len_bytes - b->len_bytes; 2019 break; 2020 2021 case 3: /* tot pkts */ 2022 res = a->tot_pkts - b->tot_pkts; 2023 break; 2024 2025 case 4: /* tot bytes */ 2026 res = a->tot_bytes - b->tot_bytes; 2027 break; 2028 } 2029 if (res < 0) 2030 res = -1; 2031 if (res > 0) 2032 res = 1; 2033 return (int)(rev ? res : -res); 2034} 2035 2036static void 2037list_queues(struct dn_flow_set *fs, struct dn_flow_queue *q) 2038{ 2039 int l; 2040 int index_printed, indexes = 0; 2041 char buff[255]; 2042 struct protoent *pe; 2043 2044 if (fs->rq_elements == 0) 2045 return; 2046 2047 if (do_sort != 0) 2048 heapsort(q, fs->rq_elements, sizeof *q, sort_q); 2049 2050 /* Print IPv4 flows */ 2051 index_printed = 0; 2052 for (l = 0; l < fs->rq_elements; l++) { 2053 struct in_addr ina; 2054 2055 /* XXX: Should check for IPv4 flows */ 2056 if (IS_IP6_FLOW_ID(&(q[l].id))) 2057 continue; 2058 2059 if (!index_printed) { 2060 index_printed = 1; 2061 if (indexes > 0) /* currently a no-op */ 2062 printf("\n"); 2063 indexes++; 2064 printf(" " 2065 "mask: 0x%02x 0x%08x/0x%04x -> 0x%08x/0x%04x\n", 2066 fs->flow_mask.proto, 2067 fs->flow_mask.src_ip, fs->flow_mask.src_port, 2068 fs->flow_mask.dst_ip, fs->flow_mask.dst_port); 2069 2070 printf("BKT Prot ___Source IP/port____ " 2071 "____Dest. IP/port____ " 2072 "Tot_pkt/bytes Pkt/Byte Drp\n"); 2073 } 2074 2075 printf("%3d ", q[l].hash_slot); 2076 pe = getprotobynumber(q[l].id.proto); 2077 if (pe) 2078 printf("%-4s ", pe->p_name); 2079 else 2080 printf("%4u ", q[l].id.proto); 2081 ina.s_addr = htonl(q[l].id.src_ip); 2082 printf("%15s/%-5d ", 2083 inet_ntoa(ina), q[l].id.src_port); 2084 ina.s_addr = htonl(q[l].id.dst_ip); 2085 printf("%15s/%-5d ", 2086 inet_ntoa(ina), q[l].id.dst_port); 2087 printf("%4qu %8qu %2u %4u %3u\n", 2088 q[l].tot_pkts, q[l].tot_bytes, 2089 q[l].len, q[l].len_bytes, q[l].drops); 2090 if (verbose) 2091 printf(" S %20qd F %20qd\n", 2092 q[l].S, q[l].F); 2093 } 2094 2095 /* Print IPv6 flows */ 2096 index_printed = 0; 2097 for (l = 0; l < fs->rq_elements; l++) { 2098 if (!IS_IP6_FLOW_ID(&(q[l].id))) 2099 continue; 2100 2101 if (!index_printed) { 2102 index_printed = 1; 2103 if (indexes > 0) 2104 printf("\n"); 2105 indexes++; 2106 printf("\n mask: proto: 0x%02x, flow_id: 0x%08x, ", 2107 fs->flow_mask.proto, fs->flow_mask.flow_id6); 2108 inet_ntop(AF_INET6, &(fs->flow_mask.src_ip6), 2109 buff, sizeof(buff)); 2110 printf("%s/0x%04x -> ", buff, fs->flow_mask.src_port); 2111 inet_ntop( AF_INET6, &(fs->flow_mask.dst_ip6), 2112 buff, sizeof(buff) ); 2113 printf("%s/0x%04x\n", buff, fs->flow_mask.dst_port); 2114 2115 printf("BKT ___Prot___ _flow-id_ " 2116 "______________Source IPv6/port_______________ " 2117 "_______________Dest. IPv6/port_______________ " 2118 "Tot_pkt/bytes Pkt/Byte Drp\n"); 2119 } 2120 printf("%3d ", q[l].hash_slot); 2121 pe = getprotobynumber(q[l].id.proto); 2122 if (pe != NULL) 2123 printf("%9s ", pe->p_name); 2124 else 2125 printf("%9u ", q[l].id.proto); 2126 printf("%7d %39s/%-5d ", q[l].id.flow_id6, 2127 inet_ntop(AF_INET6, &(q[l].id.src_ip6), buff, sizeof(buff)), 2128 q[l].id.src_port); 2129 printf(" %39s/%-5d ", 2130 inet_ntop(AF_INET6, &(q[l].id.dst_ip6), buff, sizeof(buff)), 2131 q[l].id.dst_port); 2132 printf(" %4qu %8qu %2u %4u %3u\n", 2133 q[l].tot_pkts, q[l].tot_bytes, 2134 q[l].len, q[l].len_bytes, q[l].drops); 2135 if (verbose) 2136 printf(" S %20qd F %20qd\n", q[l].S, q[l].F); 2137 } 2138} 2139 2140static void 2141print_flowset_parms(struct dn_flow_set *fs, char *prefix) 2142{ 2143 int l; 2144 char qs[30]; 2145 char plr[30]; 2146 char red[90]; /* Display RED parameters */ 2147 2148 l = fs->qsize; 2149 if (fs->flags_fs & DN_QSIZE_IS_BYTES) { 2150 if (l >= 8192) 2151 sprintf(qs, "%d KB", l / 1024); 2152 else 2153 sprintf(qs, "%d B", l); 2154 } else 2155 sprintf(qs, "%3d sl.", l); 2156 if (fs->plr) 2157 sprintf(plr, "plr %f", 1.0 * fs->plr / (double)(0x7fffffff)); 2158 else 2159 plr[0] = '\0'; 2160 if (fs->flags_fs & DN_IS_RED) /* RED parameters */ 2161 sprintf(red, 2162 "\n\t %cRED w_q %f min_th %d max_th %d max_p %f", 2163 (fs->flags_fs & DN_IS_GENTLE_RED) ? 'G' : ' ', 2164 1.0 * fs->w_q / (double)(1 << SCALE_RED), 2165 SCALE_VAL(fs->min_th), 2166 SCALE_VAL(fs->max_th), 2167 1.0 * fs->max_p / (double)(1 << SCALE_RED)); 2168 else 2169 sprintf(red, "droptail"); 2170 2171 printf("%s %s%s %d queues (%d buckets) %s\n", 2172 prefix, qs, plr, fs->rq_elements, fs->rq_size, red); 2173} 2174 2175static void 2176list_pipes(void *data, uint nbytes, int ac, char *av[]) 2177{ 2178 int rulenum; 2179 void *next = data; 2180 struct dn_pipe *p = (struct dn_pipe *) data; 2181 struct dn_flow_set *fs; 2182 struct dn_flow_queue *q; 2183 int l; 2184 2185 if (ac > 0) 2186 rulenum = strtoul(*av++, NULL, 10); 2187 else 2188 rulenum = 0; 2189 for (; nbytes >= sizeof *p; p = (struct dn_pipe *)next) { 2190 double b = p->bandwidth; 2191 char buf[30]; 2192 char prefix[80]; 2193 2194 if (p->next.sle_next != (struct dn_pipe *)DN_IS_PIPE) 2195 break; /* done with pipes, now queues */ 2196 2197 /* 2198 * compute length, as pipe have variable size 2199 */ 2200 l = sizeof(*p) + p->fs.rq_elements * sizeof(*q); 2201 next = (char *)p + l; 2202 nbytes -= l; 2203 2204 if ((rulenum != 0 && rulenum != p->pipe_nr) || do_pipe == 2) 2205 continue; 2206 2207 /* 2208 * Print rate (or clocking interface) 2209 */ 2210 if (p->if_name[0] != '\0') 2211 sprintf(buf, "%s", p->if_name); 2212 else if (b == 0) 2213 sprintf(buf, "unlimited"); 2214 else if (b >= 1000000) 2215 sprintf(buf, "%7.3f Mbit/s", b/1000000); 2216 else if (b >= 1000) 2217 sprintf(buf, "%7.3f Kbit/s", b/1000); 2218 else 2219 sprintf(buf, "%7.3f bit/s ", b); 2220 2221 sprintf(prefix, "%05d: %s %4d ms ", 2222 p->pipe_nr, buf, p->delay); 2223 print_flowset_parms(&(p->fs), prefix); 2224 if (verbose) 2225 printf(" V %20qd\n", p->V >> MY_M); 2226 2227 q = (struct dn_flow_queue *)(p+1); 2228 list_queues(&(p->fs), q); 2229 } 2230 for (fs = next; nbytes >= sizeof *fs; fs = next) { 2231 char prefix[80]; 2232 2233 if (fs->next.sle_next != (struct dn_flow_set *)DN_IS_QUEUE) 2234 break; 2235 l = sizeof(*fs) + fs->rq_elements * sizeof(*q); 2236 next = (char *)fs + l; 2237 nbytes -= l; 2238 2239 if (rulenum != 0 && ((rulenum != fs->fs_nr && do_pipe == 2) || 2240 (rulenum != fs->parent_nr && do_pipe == 1))) { 2241 continue; 2242 } 2243 2244 q = (struct dn_flow_queue *)(fs+1); 2245 sprintf(prefix, "q%05d: weight %d pipe %d ", 2246 fs->fs_nr, fs->weight, fs->parent_nr); 2247 print_flowset_parms(fs, prefix); 2248 list_queues(fs, q); 2249 } 2250} 2251 2252/* 2253 * This one handles all set-related commands 2254 * ipfw set { show | enable | disable } 2255 * ipfw set swap X Y 2256 * ipfw set move X to Y 2257 * ipfw set move rule X to Y 2258 */ 2259static void 2260sets_handler(int ac, char *av[]) 2261{ 2262 uint32_t set_disable, masks[2]; 2263 int i, nbytes; 2264 uint16_t rulenum; 2265 uint8_t cmd, new_set; 2266 2267 ac--; 2268 av++; 2269 2270 if (!ac) 2271 errx(EX_USAGE, "set needs command"); 2272 if (_substrcmp(*av, "show") == 0) { 2273 void *data; 2274 char const *msg; 2275 2276 nbytes = sizeof(struct ip_fw); 2277 if ((data = calloc(1, nbytes)) == NULL) 2278 err(EX_OSERR, "calloc"); 2279 if (do_cmd(IP_FW_GET, data, (uintptr_t)&nbytes) < 0) 2280 err(EX_OSERR, "getsockopt(IP_FW_GET)"); 2281 bcopy(&((struct ip_fw *)data)->next_rule, 2282 &set_disable, sizeof(set_disable)); 2283 2284 for (i = 0, msg = "disable" ; i < RESVD_SET; i++) 2285 if ((set_disable & (1<<i))) { 2286 printf("%s %d", msg, i); 2287 msg = ""; 2288 } 2289 msg = (set_disable) ? " enable" : "enable"; 2290 for (i = 0; i < RESVD_SET; i++) 2291 if (!(set_disable & (1<<i))) { 2292 printf("%s %d", msg, i); 2293 msg = ""; 2294 } 2295 printf("\n"); 2296 } else if (_substrcmp(*av, "swap") == 0) { 2297 ac--; av++; 2298 if (ac != 2) 2299 errx(EX_USAGE, "set swap needs 2 set numbers\n"); 2300 rulenum = atoi(av[0]); 2301 new_set = atoi(av[1]); 2302 if (!isdigit(*(av[0])) || rulenum > RESVD_SET) 2303 errx(EX_DATAERR, "invalid set number %s\n", av[0]); 2304 if (!isdigit(*(av[1])) || new_set > RESVD_SET) 2305 errx(EX_DATAERR, "invalid set number %s\n", av[1]); 2306 masks[0] = (4 << 24) | (new_set << 16) | (rulenum); 2307 i = do_cmd(IP_FW_DEL, masks, sizeof(uint32_t)); 2308 } else if (_substrcmp(*av, "move") == 0) { 2309 ac--; av++; 2310 if (ac && _substrcmp(*av, "rule") == 0) { 2311 cmd = 2; 2312 ac--; av++; 2313 } else 2314 cmd = 3; 2315 if (ac != 3 || _substrcmp(av[1], "to") != 0) 2316 errx(EX_USAGE, "syntax: set move [rule] X to Y\n"); 2317 rulenum = atoi(av[0]); 2318 new_set = atoi(av[2]); 2319 if (!isdigit(*(av[0])) || (cmd == 3 && rulenum > RESVD_SET) || 2320 (cmd == 2 && rulenum == 65535) ) 2321 errx(EX_DATAERR, "invalid source number %s\n", av[0]); 2322 if (!isdigit(*(av[2])) || new_set > RESVD_SET) 2323 errx(EX_DATAERR, "invalid dest. set %s\n", av[1]); 2324 masks[0] = (cmd << 24) | (new_set << 16) | (rulenum); 2325 i = do_cmd(IP_FW_DEL, masks, sizeof(uint32_t)); 2326 } else if (_substrcmp(*av, "disable") == 0 || 2327 _substrcmp(*av, "enable") == 0 ) { 2328 int which = _substrcmp(*av, "enable") == 0 ? 1 : 0; 2329 2330 ac--; av++; 2331 masks[0] = masks[1] = 0; 2332 2333 while (ac) { 2334 if (isdigit(**av)) { 2335 i = atoi(*av); 2336 if (i < 0 || i > RESVD_SET) 2337 errx(EX_DATAERR, 2338 "invalid set number %d\n", i); 2339 masks[which] |= (1<<i); 2340 } else if (_substrcmp(*av, "disable") == 0) 2341 which = 0; 2342 else if (_substrcmp(*av, "enable") == 0) 2343 which = 1; 2344 else 2345 errx(EX_DATAERR, 2346 "invalid set command %s\n", *av); 2347 av++; ac--; 2348 } 2349 if ( (masks[0] & masks[1]) != 0 ) 2350 errx(EX_DATAERR, 2351 "cannot enable and disable the same set\n"); 2352 2353 i = do_cmd(IP_FW_DEL, masks, sizeof(masks)); 2354 if (i) 2355 warn("set enable/disable: setsockopt(IP_FW_DEL)"); 2356 } else 2357 errx(EX_USAGE, "invalid set command %s\n", *av); 2358} 2359 2360static void 2361sysctl_handler(int ac, char *av[], int which) 2362{ 2363 ac--; 2364 av++; 2365 2366 if (ac == 0) { 2367 warnx("missing keyword to enable/disable\n"); 2368 } else if (_substrcmp(*av, "firewall") == 0) { 2369 sysctlbyname("net.inet.ip.fw.enable", NULL, 0, 2370 &which, sizeof(which)); 2371 } else if (_substrcmp(*av, "one_pass") == 0) { 2372 sysctlbyname("net.inet.ip.fw.one_pass", NULL, 0, 2373 &which, sizeof(which)); 2374 } else if (_substrcmp(*av, "debug") == 0) { 2375 sysctlbyname("net.inet.ip.fw.debug", NULL, 0, 2376 &which, sizeof(which)); 2377 } else if (_substrcmp(*av, "verbose") == 0) { 2378 sysctlbyname("net.inet.ip.fw.verbose", NULL, 0, 2379 &which, sizeof(which)); 2380 } else if (_substrcmp(*av, "dyn_keepalive") == 0) { 2381 sysctlbyname("net.inet.ip.fw.dyn_keepalive", NULL, 0, 2382 &which, sizeof(which)); 2383 } else if (_substrcmp(*av, "altq") == 0) { 2384 altq_set_enabled(which); 2385 } else { 2386 warnx("unrecognize enable/disable keyword: %s\n", *av); 2387 } 2388} 2389 2390static void 2391list(int ac, char *av[], int show_counters) 2392{ 2393 struct ip_fw *r; 2394 ipfw_dyn_rule *dynrules, *d; 2395 2396#define NEXT(r) ((struct ip_fw *)((char *)r + RULESIZE(r))) 2397 char *lim; 2398 void *data = NULL; 2399 int bcwidth, n, nbytes, nstat, ndyn, pcwidth, width; 2400 int exitval = EX_OK; 2401 int lac; 2402 char **lav; 2403 u_long rnum, last; 2404 char *endptr; 2405 int seen = 0; 2406 2407 const int ocmd = do_pipe ? IP_DUMMYNET_GET : IP_FW_GET; 2408 int nalloc = 1024; /* start somewhere... */ 2409 2410 last = 0; 2411 2412 if (test_only) { 2413 fprintf(stderr, "Testing only, list disabled\n"); 2414 return; 2415 } 2416 2417 ac--; 2418 av++; 2419 2420 /* get rules or pipes from kernel, resizing array as necessary */ 2421 nbytes = nalloc; 2422 2423 while (nbytes >= nalloc) { 2424 nalloc = nalloc * 2 + 200; 2425 nbytes = nalloc; 2426 if ((data = realloc(data, nbytes)) == NULL) 2427 err(EX_OSERR, "realloc"); 2428 if (do_cmd(ocmd, data, (uintptr_t)&nbytes) < 0) 2429 err(EX_OSERR, "getsockopt(IP_%s_GET)", 2430 do_pipe ? "DUMMYNET" : "FW"); 2431 } 2432 2433 if (do_pipe) { 2434 list_pipes(data, nbytes, ac, av); 2435 goto done; 2436 } 2437 2438 /* 2439 * Count static rules. They have variable size so we 2440 * need to scan the list to count them. 2441 */ 2442 for (nstat = 1, r = data, lim = (char *)data + nbytes; 2443 r->rulenum < 65535 && (char *)r < lim; 2444 ++nstat, r = NEXT(r) ) 2445 ; /* nothing */ 2446 2447 /* 2448 * Count dynamic rules. This is easier as they have 2449 * fixed size. 2450 */ 2451 r = NEXT(r); 2452 dynrules = (ipfw_dyn_rule *)r ; 2453 n = (char *)r - (char *)data; 2454 ndyn = (nbytes - n) / sizeof *dynrules; 2455 2456 /* if showing stats, figure out column widths ahead of time */ 2457 bcwidth = pcwidth = 0; 2458 if (show_counters) { 2459 for (n = 0, r = data; n < nstat; n++, r = NEXT(r)) { 2460 /* packet counter */ 2461 width = snprintf(NULL, 0, "%llu", 2462 align_uint64(&r->pcnt)); 2463 if (width > pcwidth) 2464 pcwidth = width; 2465 2466 /* byte counter */ 2467 width = snprintf(NULL, 0, "%llu", 2468 align_uint64(&r->bcnt)); 2469 if (width > bcwidth) 2470 bcwidth = width; 2471 } 2472 } 2473 if (do_dynamic && ndyn) { 2474 for (n = 0, d = dynrules; n < ndyn; n++, d++) { 2475 width = snprintf(NULL, 0, "%llu", 2476 align_uint64(&d->pcnt)); 2477 if (width > pcwidth) 2478 pcwidth = width; 2479 2480 width = snprintf(NULL, 0, "%llu", 2481 align_uint64(&d->bcnt)); 2482 if (width > bcwidth) 2483 bcwidth = width; 2484 } 2485 } 2486 /* if no rule numbers were specified, list all rules */ 2487 if (ac == 0) { 2488 for (n = 0, r = data; n < nstat; n++, r = NEXT(r) ) 2489 show_ipfw(r, pcwidth, bcwidth); 2490 2491 if (do_dynamic && ndyn) { 2492 printf("## Dynamic rules (%d):\n", ndyn); 2493 for (n = 0, d = dynrules; n < ndyn; n++, d++) 2494 show_dyn_ipfw(d, pcwidth, bcwidth); 2495 } 2496 goto done; 2497 } 2498 2499 /* display specific rules requested on command line */ 2500 2501 for (lac = ac, lav = av; lac != 0; lac--) { 2502 /* convert command line rule # */ 2503 last = rnum = strtoul(*lav++, &endptr, 10); 2504 if (*endptr == '-') 2505 last = strtoul(endptr+1, &endptr, 10); 2506 if (*endptr) { 2507 exitval = EX_USAGE; 2508 warnx("invalid rule number: %s", *(lav - 1)); 2509 continue; 2510 } 2511 for (n = seen = 0, r = data; n < nstat; n++, r = NEXT(r) ) { 2512 if (r->rulenum > last) 2513 break; 2514 if (r->rulenum >= rnum && r->rulenum <= last) { 2515 show_ipfw(r, pcwidth, bcwidth); 2516 seen = 1; 2517 } 2518 } 2519 if (!seen) { 2520 /* give precedence to other error(s) */ 2521 if (exitval == EX_OK) 2522 exitval = EX_UNAVAILABLE; 2523 warnx("rule %lu does not exist", rnum); 2524 } 2525 } 2526 2527 if (do_dynamic && ndyn) { 2528 printf("## Dynamic rules:\n"); 2529 for (lac = ac, lav = av; lac != 0; lac--) { 2530 last = rnum = strtoul(*lav++, &endptr, 10); 2531 if (*endptr == '-') 2532 last = strtoul(endptr+1, &endptr, 10); 2533 if (*endptr) 2534 /* already warned */ 2535 continue; 2536 for (n = 0, d = dynrules; n < ndyn; n++, d++) { 2537 uint16_t rulenum; 2538 2539 bcopy(&d->rule, &rulenum, sizeof(rulenum)); 2540 if (rulenum > rnum) 2541 break; 2542 if (r->rulenum >= rnum && r->rulenum <= last) 2543 show_dyn_ipfw(d, pcwidth, bcwidth); 2544 } 2545 } 2546 } 2547 2548 ac = 0; 2549 2550done: 2551 free(data); 2552 2553 if (exitval != EX_OK) 2554 exit(exitval); 2555#undef NEXT 2556} 2557 2558static void 2559show_usage(void) 2560{ 2561 fprintf(stderr, "usage: ipfw [options]\n" 2562"do \"ipfw -h\" or see ipfw manpage for details\n" 2563); 2564 exit(EX_USAGE); 2565} 2566 2567static void 2568help(void) 2569{ 2570 fprintf(stderr, 2571"ipfw syntax summary (but please do read the ipfw(8) manpage):\n" 2572"ipfw [-abcdefhnNqStTv] <command> where <command> is one of:\n" 2573"add [num] [set N] [prob x] RULE-BODY\n" 2574"{pipe|queue} N config PIPE-BODY\n" 2575"[pipe|queue] {zero|delete|show} [N{,N}]\n" 2576"set [disable N... enable N...] | move [rule] X to Y | swap X Y | show\n" 2577"table N {add ip[/bits] [value] | delete ip[/bits] | flush | list}\n" 2578"\n" 2579"RULE-BODY: check-state [PARAMS] | ACTION [PARAMS] ADDR [OPTION_LIST]\n" 2580"ACTION: check-state | allow | count | deny | unreach{,6} CODE |\n" 2581" skipto N | {divert|tee} PORT | forward ADDR |\n" 2582" pipe N | queue N\n" 2583"PARAMS: [log [logamount LOGLIMIT]] [altq QUEUE_NAME]\n" 2584"ADDR: [ MAC dst src ether_type ] \n" 2585" [ ip from IPADDR [ PORT ] to IPADDR [ PORTLIST ] ]\n" 2586" [ ipv6|ip6 from IP6ADDR [ PORT ] to IP6ADDR [ PORTLIST ] ]\n" 2587"IPADDR: [not] { any | me | ip/bits{x,y,z} | table(t[,v]) | IPLIST }\n" 2588"IP6ADDR: [not] { any | me | me6 | ip6/bits | IP6LIST }\n" 2589"IP6LIST: { ip6 | ip6/bits }[,IP6LIST]\n" 2590"IPLIST: { ip | ip/bits | ip:mask }[,IPLIST]\n" 2591"OPTION_LIST: OPTION [OPTION_LIST]\n" 2592"OPTION: bridged | diverted | diverted-loopback | diverted-output |\n" 2593" {dst-ip|src-ip} IPADDR | {dst-ip6|src-ip6|dst-ipv6|src-ipv6} IP6ADDR |\n" 2594" {dst-port|src-port} LIST |\n" 2595" estab | frag | {gid|uid} N | icmptypes LIST | in | out | ipid LIST |\n" 2596" iplen LIST | ipoptions SPEC | ipprecedence | ipsec | iptos SPEC |\n" 2597" ipttl LIST | ipversion VER | keep-state | layer2 | limit ... |\n" 2598" icmp6types LIST | ext6hdr LIST | flow-id N[,N] |\n" 2599" mac ... | mac-type LIST | proto LIST | {recv|xmit|via} {IF|IPADDR} |\n" 2600" setup | {tcpack|tcpseq|tcpwin} NN | tcpflags SPEC | tcpoptions SPEC |\n" 2601" tcpdatalen LIST | verrevpath | versrcreach | antispoof\n" 2602); 2603exit(0); 2604} 2605 2606 2607static int 2608lookup_host (char *host, struct in_addr *ipaddr) 2609{ 2610 struct hostent *he; 2611 2612 if (!inet_aton(host, ipaddr)) { 2613 if ((he = gethostbyname(host)) == NULL) 2614 return(-1); 2615 *ipaddr = *(struct in_addr *)he->h_addr_list[0]; 2616 } 2617 return(0); 2618} 2619 2620/* 2621 * fills the addr and mask fields in the instruction as appropriate from av. 2622 * Update length as appropriate. 2623 * The following formats are allowed: 2624 * me returns O_IP_*_ME 2625 * 1.2.3.4 single IP address 2626 * 1.2.3.4:5.6.7.8 address:mask 2627 * 1.2.3.4/24 address/mask 2628 * 1.2.3.4/26{1,6,5,4,23} set of addresses in a subnet 2629 * We can have multiple comma-separated address/mask entries. 2630 */ 2631static void 2632fill_ip(ipfw_insn_ip *cmd, char *av) 2633{ 2634 int len = 0; 2635 uint32_t *d = ((ipfw_insn_u32 *)cmd)->d; 2636 2637 cmd->o.len &= ~F_LEN_MASK; /* zero len */ 2638 2639 if (_substrcmp(av, "any") == 0) 2640 return; 2641 2642 if (_substrcmp(av, "me") == 0) { 2643 cmd->o.len |= F_INSN_SIZE(ipfw_insn); 2644 return; 2645 } 2646 2647 if (strncmp(av, "table(", 6) == 0) { 2648 char *p = strchr(av + 6, ','); 2649 2650 if (p) 2651 *p++ = '\0'; 2652 cmd->o.opcode = O_IP_DST_LOOKUP; 2653 cmd->o.arg1 = strtoul(av + 6, NULL, 0); 2654 if (p) { 2655 cmd->o.len |= F_INSN_SIZE(ipfw_insn_u32); 2656 d[0] = strtoul(p, NULL, 0); 2657 } else 2658 cmd->o.len |= F_INSN_SIZE(ipfw_insn); 2659 return; 2660 } 2661 2662 while (av) { 2663 /* 2664 * After the address we can have '/' or ':' indicating a mask, 2665 * ',' indicating another address follows, '{' indicating a 2666 * set of addresses of unspecified size. 2667 */ 2668 char *p = strpbrk(av, "/:,{"); 2669 int masklen; 2670 char md; 2671 2672 if (p) { 2673 md = *p; 2674 *p++ = '\0'; 2675 } else 2676 md = '\0'; 2677 2678 if (lookup_host(av, (struct in_addr *)&d[0]) != 0) 2679 errx(EX_NOHOST, "hostname ``%s'' unknown", av); 2680 switch (md) { 2681 case ':': 2682 if (!inet_aton(p, (struct in_addr *)&d[1])) 2683 errx(EX_DATAERR, "bad netmask ``%s''", p); 2684 break; 2685 case '/': 2686 masklen = atoi(p); 2687 if (masklen == 0) 2688 d[1] = htonl(0); /* mask */ 2689 else if (masklen > 32) 2690 errx(EX_DATAERR, "bad width ``%s''", p); 2691 else 2692 d[1] = htonl(~0 << (32 - masklen)); 2693 break; 2694 case '{': /* no mask, assume /24 and put back the '{' */ 2695 d[1] = htonl(~0 << (32 - 24)); 2696 *(--p) = md; 2697 break; 2698 2699 case ',': /* single address plus continuation */ 2700 *(--p) = md; 2701 /* FALLTHROUGH */ 2702 case 0: /* initialization value */ 2703 default: 2704 d[1] = htonl(~0); /* force /32 */ 2705 break; 2706 } 2707 d[0] &= d[1]; /* mask base address with mask */ 2708 /* find next separator */ 2709 if (p) 2710 p = strpbrk(p, ",{"); 2711 if (p && *p == '{') { 2712 /* 2713 * We have a set of addresses. They are stored as follows: 2714 * arg1 is the set size (powers of 2, 2..256) 2715 * addr is the base address IN HOST FORMAT 2716 * mask.. is an array of arg1 bits (rounded up to 2717 * the next multiple of 32) with bits set 2718 * for each host in the map. 2719 */ 2720 uint32_t *map = (uint32_t *)&cmd->mask; 2721 int low, high; 2722 int i = contigmask((uint8_t *)&(d[1]), 32); 2723 2724 if (len > 0) 2725 errx(EX_DATAERR, "address set cannot be in a list"); 2726 if (i < 24 || i > 31) 2727 errx(EX_DATAERR, "invalid set with mask %d\n", i); 2728 cmd->o.arg1 = 1<<(32-i); /* map length */ 2729 d[0] = ntohl(d[0]); /* base addr in host format */ 2730 cmd->o.opcode = O_IP_DST_SET; /* default */ 2731 cmd->o.len |= F_INSN_SIZE(ipfw_insn_u32) + (cmd->o.arg1+31)/32; 2732 for (i = 0; i < (cmd->o.arg1+31)/32 ; i++) 2733 map[i] = 0; /* clear map */ 2734 2735 av = p + 1; 2736 low = d[0] & 0xff; 2737 high = low + cmd->o.arg1 - 1; 2738 /* 2739 * Here, i stores the previous value when we specify a range 2740 * of addresses within a mask, e.g. 45-63. i = -1 means we 2741 * have no previous value. 2742 */ 2743 i = -1; /* previous value in a range */ 2744 while (isdigit(*av)) { 2745 char *s; 2746 int a = strtol(av, &s, 0); 2747 2748 if (s == av) { /* no parameter */ 2749 if (*av != '}') 2750 errx(EX_DATAERR, "set not closed\n"); 2751 if (i != -1) 2752 errx(EX_DATAERR, "incomplete range %d-", i); 2753 break; 2754 } 2755 if (a < low || a > high) 2756 errx(EX_DATAERR, "addr %d out of range [%d-%d]\n", 2757 a, low, high); 2758 a -= low; 2759 if (i == -1) /* no previous in range */ 2760 i = a; 2761 else { /* check that range is valid */ 2762 if (i > a) 2763 errx(EX_DATAERR, "invalid range %d-%d", 2764 i+low, a+low); 2765 if (*s == '-') 2766 errx(EX_DATAERR, "double '-' in range"); 2767 } 2768 for (; i <= a; i++) 2769 map[i/32] |= 1<<(i & 31); 2770 i = -1; 2771 if (*s == '-') 2772 i = a; 2773 else if (*s == '}') 2774 break; 2775 av = s+1; 2776 } 2777 return; 2778 } 2779 av = p; 2780 if (av) /* then *av must be a ',' */ 2781 av++; 2782 2783 /* Check this entry */ 2784 if (d[1] == 0) { /* "any", specified as x.x.x.x/0 */ 2785 /* 2786 * 'any' turns the entire list into a NOP. 2787 * 'not any' never matches, so it is removed from the 2788 * list unless it is the only item, in which case we 2789 * report an error. 2790 */ 2791 if (cmd->o.len & F_NOT) { /* "not any" never matches */ 2792 if (av == NULL && len == 0) /* only this entry */ 2793 errx(EX_DATAERR, "not any never matches"); 2794 } 2795 /* else do nothing and skip this entry */ 2796 return; 2797 } 2798 /* A single IP can be stored in an optimized format */ 2799 if (d[1] == IP_MASK_ALL && av == NULL && len == 0) { 2800 cmd->o.len |= F_INSN_SIZE(ipfw_insn_u32); 2801 return; 2802 } 2803 len += 2; /* two words... */ 2804 d += 2; 2805 } /* end while */ 2806 cmd->o.len |= len+1; 2807} 2808 2809 2810/* Try to find ipv6 address by hostname */ 2811static int 2812lookup_host6 (char *host, struct in6_addr *ip6addr) 2813{ 2814 struct hostent *he; 2815 2816 if (!inet_pton(AF_INET6, host, ip6addr)) { 2817 if ((he = gethostbyname2(host, AF_INET6)) == NULL) 2818 return(-1); 2819 memcpy(ip6addr, he->h_addr_list[0], sizeof( struct in6_addr)); 2820 } 2821 return(0); 2822} 2823 2824 2825/* n2mask sets n bits of the mask */ 2826static void 2827n2mask(struct in6_addr *mask, int n) 2828{ 2829 static int minimask[9] = 2830 { 0x00, 0x80, 0xc0, 0xe0, 0xf0, 0xf8, 0xfc, 0xfe, 0xff }; 2831 u_char *p; 2832 2833 memset(mask, 0, sizeof(struct in6_addr)); 2834 p = (u_char *) mask; 2835 for (; n > 0; p++, n -= 8) { 2836 if (n >= 8) 2837 *p = 0xff; 2838 else 2839 *p = minimask[n]; 2840 } 2841 return; 2842} 2843 2844 2845/* 2846 * fill the addr and mask fields in the instruction as appropriate from av. 2847 * Update length as appropriate. 2848 * The following formats are allowed: 2849 * any matches any IP6. Actually returns an empty instruction. 2850 * me returns O_IP6_*_ME 2851 * 2852 * 03f1::234:123:0342 single IP6 addres 2853 * 03f1::234:123:0342/24 address/mask 2854 * 03f1::234:123:0342/24,03f1::234:123:0343/ List of address 2855 * 2856 * Set of address (as in ipv6) not supported because ipv6 address 2857 * are typically random past the initial prefix. 2858 * Return 1 on success, 0 on failure. 2859 */ 2860static int 2861fill_ip6(ipfw_insn_ip6 *cmd, char *av) 2862{ 2863 int len = 0; 2864 struct in6_addr *d = &(cmd->addr6); 2865 /* 2866 * Needed for multiple address. 2867 * Note d[1] points to struct in6_add r mask6 of cmd 2868 */ 2869 2870 cmd->o.len &= ~F_LEN_MASK; /* zero len */ 2871 2872 if (strcmp(av, "any") == 0) 2873 return (1); 2874 2875 2876 if (strcmp(av, "me") == 0) { /* Set the data for "me" opt*/ 2877 cmd->o.len |= F_INSN_SIZE(ipfw_insn); 2878 return (1); 2879 } 2880 2881 if (strcmp(av, "me6") == 0) { /* Set the data for "me" opt*/ 2882 cmd->o.len |= F_INSN_SIZE(ipfw_insn); 2883 return (1); 2884 } 2885 2886 av = strdup(av); 2887 while (av) { 2888 /* 2889 * After the address we can have '/' indicating a mask, 2890 * or ',' indicating another address follows. 2891 */ 2892 2893 char *p; 2894 int masklen; 2895 char md = '\0'; 2896 2897 if ((p = strpbrk(av, "/,")) ) { 2898 md = *p; /* save the separator */ 2899 *p = '\0'; /* terminate address string */ 2900 p++; /* and skip past it */ 2901 } 2902 /* now p points to NULL, mask or next entry */ 2903 2904 /* lookup stores address in *d as a side effect */ 2905 if (lookup_host6(av, d) != 0) { 2906 /* XXX: failed. Free memory and go */ 2907 errx(EX_DATAERR, "bad address \"%s\"", av); 2908 } 2909 /* next, look at the mask, if any */ 2910 masklen = (md == '/') ? atoi(p) : 128; 2911 if (masklen > 128 || masklen < 0) 2912 errx(EX_DATAERR, "bad width \"%s\''", p); 2913 else 2914 n2mask(&d[1], masklen); 2915 2916 APPLY_MASK(d, &d[1]) /* mask base address with mask */ 2917 2918 /* find next separator */ 2919 2920 if (md == '/') { /* find separator past the mask */ 2921 p = strpbrk(p, ","); 2922 if (p != NULL) 2923 p++; 2924 } 2925 av = p; 2926 2927 /* Check this entry */ 2928 if (masklen == 0) { 2929 /* 2930 * 'any' turns the entire list into a NOP. 2931 * 'not any' never matches, so it is removed from the 2932 * list unless it is the only item, in which case we 2933 * report an error. 2934 */ 2935 if (cmd->o.len & F_NOT && av == NULL && len == 0) 2936 errx(EX_DATAERR, "not any never matches"); 2937 continue; 2938 } 2939 2940 /* 2941 * A single IP can be stored alone 2942 */ 2943 if (masklen == 128 && av == NULL && len == 0) { 2944 len = F_INSN_SIZE(struct in6_addr); 2945 break; 2946 } 2947 2948 /* Update length and pointer to arguments */ 2949 len += F_INSN_SIZE(struct in6_addr)*2; 2950 d += 2; 2951 } /* end while */ 2952 2953 /* 2954 * Total length of the command, remember that 1 is the size of 2955 * the base command. 2956 */ 2957 cmd->o.len |= len+1; 2958 free(av); 2959 return (1); 2960} 2961 2962/* 2963 * fills command for ipv6 flow-id filtering 2964 * note that the 20 bit flow number is stored in a array of u_int32_t 2965 * it's supported lists of flow-id, so in the o.arg1 we store how many 2966 * additional flow-id we want to filter, the basic is 1 2967 */ 2968void 2969fill_flow6( ipfw_insn_u32 *cmd, char *av ) 2970{ 2971 u_int32_t type; /* Current flow number */ 2972 u_int16_t nflow = 0; /* Current flow index */ 2973 char *s = av; 2974 cmd->d[0] = 0; /* Initializing the base number*/ 2975 2976 while (s) { 2977 av = strsep( &s, ",") ; 2978 type = strtoul(av, &av, 0); 2979 if (*av != ',' && *av != '\0') 2980 errx(EX_DATAERR, "invalid ipv6 flow number %s", av); 2981 if (type > 0xfffff) 2982 errx(EX_DATAERR, "flow number out of range %s", av); 2983 cmd->d[nflow] |= type; 2984 nflow++; 2985 } 2986 if( nflow > 0 ) { 2987 cmd->o.opcode = O_FLOW6ID; 2988 cmd->o.len |= F_INSN_SIZE(ipfw_insn_u32) + nflow; 2989 cmd->o.arg1 = nflow; 2990 } 2991 else { 2992 errx(EX_DATAERR, "invalid ipv6 flow number %s", av); 2993 } 2994} 2995 2996static ipfw_insn * 2997add_srcip6(ipfw_insn *cmd, char *av) 2998{ 2999 3000 fill_ip6((ipfw_insn_ip6 *)cmd, av); 3001 if (F_LEN(cmd) == 0) /* any */ 3002 ; 3003 if (F_LEN(cmd) == F_INSN_SIZE(ipfw_insn)) { /* "me" */ 3004 cmd->opcode = O_IP6_SRC_ME; 3005 } else if (F_LEN(cmd) == 3006 (F_INSN_SIZE(struct in6_addr) + F_INSN_SIZE(ipfw_insn))) { 3007 /* single IP, no mask*/ 3008 cmd->opcode = O_IP6_SRC; 3009 } else { /* addr/mask opt */ 3010 cmd->opcode = O_IP6_SRC_MASK; 3011 } 3012 return cmd; 3013} 3014 3015static ipfw_insn * 3016add_dstip6(ipfw_insn *cmd, char *av) 3017{ 3018 3019 fill_ip6((ipfw_insn_ip6 *)cmd, av); 3020 if (F_LEN(cmd) == 0) /* any */ 3021 ; 3022 if (F_LEN(cmd) == F_INSN_SIZE(ipfw_insn)) { /* "me" */ 3023 cmd->opcode = O_IP6_DST_ME; 3024 } else if (F_LEN(cmd) == 3025 (F_INSN_SIZE(struct in6_addr) + F_INSN_SIZE(ipfw_insn))) { 3026 /* single IP, no mask*/ 3027 cmd->opcode = O_IP6_DST; 3028 } else { /* addr/mask opt */ 3029 cmd->opcode = O_IP6_DST_MASK; 3030 } 3031 return cmd; 3032} 3033 3034 3035/* 3036 * helper function to process a set of flags and set bits in the 3037 * appropriate masks. 3038 */ 3039static void 3040fill_flags(ipfw_insn *cmd, enum ipfw_opcodes opcode, 3041 struct _s_x *flags, char *p) 3042{ 3043 uint8_t set=0, clear=0; 3044 3045 while (p && *p) { 3046 char *q; /* points to the separator */ 3047 int val; 3048 uint8_t *which; /* mask we are working on */ 3049 3050 if (*p == '!') { 3051 p++; 3052 which = &clear; 3053 } else 3054 which = &set; 3055 q = strchr(p, ','); 3056 if (q) 3057 *q++ = '\0'; 3058 val = match_token(flags, p); 3059 if (val <= 0) 3060 errx(EX_DATAERR, "invalid flag %s", p); 3061 *which |= (uint8_t)val; 3062 p = q; 3063 } 3064 cmd->opcode = opcode; 3065 cmd->len = (cmd->len & (F_NOT | F_OR)) | 1; 3066 cmd->arg1 = (set & 0xff) | ( (clear & 0xff) << 8); 3067} 3068 3069 3070static void 3071delete(int ac, char *av[]) 3072{ 3073 uint32_t rulenum; 3074 struct dn_pipe p; 3075 int i; 3076 int exitval = EX_OK; 3077 int do_set = 0; 3078 3079 memset(&p, 0, sizeof p); 3080 3081 av++; ac--; 3082 NEED1("missing rule specification"); 3083 if (ac > 0 && _substrcmp(*av, "set") == 0) { 3084 do_set = 1; /* delete set */ 3085 ac--; av++; 3086 } 3087 3088 /* Rule number */ 3089 while (ac && isdigit(**av)) { 3090 i = atoi(*av); av++; ac--; 3091 if (do_pipe) { 3092 if (do_pipe == 1) 3093 p.pipe_nr = i; 3094 else 3095 p.fs.fs_nr = i; 3096 i = do_cmd(IP_DUMMYNET_DEL, &p, sizeof p); 3097 if (i) { 3098 exitval = 1; 3099 warn("rule %u: setsockopt(IP_DUMMYNET_DEL)", 3100 do_pipe == 1 ? p.pipe_nr : p.fs.fs_nr); 3101 } 3102 } else { 3103 rulenum = (i & 0xffff) | (do_set << 24); 3104 i = do_cmd(IP_FW_DEL, &rulenum, sizeof rulenum); 3105 if (i) { 3106 exitval = EX_UNAVAILABLE; 3107 warn("rule %u: setsockopt(IP_FW_DEL)", 3108 rulenum); 3109 } 3110 } 3111 } 3112 if (exitval != EX_OK) 3113 exit(exitval); 3114} 3115 3116 3117/* 3118 * fill the interface structure. We do not check the name as we can 3119 * create interfaces dynamically, so checking them at insert time 3120 * makes relatively little sense. 3121 * Interface names containing '*', '?', or '[' are assumed to be shell 3122 * patterns which match interfaces. 3123 */ 3124static void 3125fill_iface(ipfw_insn_if *cmd, char *arg) 3126{ 3127 cmd->name[0] = '\0'; 3128 cmd->o.len |= F_INSN_SIZE(ipfw_insn_if); 3129 3130 /* Parse the interface or address */ 3131 if (strcmp(arg, "any") == 0) 3132 cmd->o.len = 0; /* effectively ignore this command */ 3133 else if (!isdigit(*arg)) { 3134 strlcpy(cmd->name, arg, sizeof(cmd->name)); 3135 cmd->p.glob = strpbrk(arg, "*?[") != NULL ? 1 : 0; 3136 } else if (!inet_aton(arg, &cmd->p.ip)) 3137 errx(EX_DATAERR, "bad ip address ``%s''", arg); 3138} 3139 3140static void 3141config_pipe(int ac, char **av) 3142{ 3143 struct dn_pipe p; 3144 int i; 3145 char *end; 3146 void *par = NULL; 3147 3148 memset(&p, 0, sizeof p); 3149 3150 av++; ac--; 3151 /* Pipe number */ 3152 if (ac && isdigit(**av)) { 3153 i = atoi(*av); av++; ac--; 3154 if (do_pipe == 1) 3155 p.pipe_nr = i; 3156 else 3157 p.fs.fs_nr = i; 3158 } 3159 while (ac > 0) { 3160 double d; 3161 int tok = match_token(dummynet_params, *av); 3162 ac--; av++; 3163 3164 switch(tok) { 3165 case TOK_NOERROR: 3166 p.fs.flags_fs |= DN_NOERROR; 3167 break; 3168 3169 case TOK_PLR: 3170 NEED1("plr needs argument 0..1\n"); 3171 d = strtod(av[0], NULL); 3172 if (d > 1) 3173 d = 1; 3174 else if (d < 0) 3175 d = 0; 3176 p.fs.plr = (int)(d*0x7fffffff); 3177 ac--; av++; 3178 break; 3179 3180 case TOK_QUEUE: 3181 NEED1("queue needs queue size\n"); 3182 end = NULL; 3183 p.fs.qsize = strtoul(av[0], &end, 0); 3184 if (*end == 'K' || *end == 'k') { 3185 p.fs.flags_fs |= DN_QSIZE_IS_BYTES; 3186 p.fs.qsize *= 1024; 3187 } else if (*end == 'B' || 3188 _substrcmp2(end, "by", "bytes") == 0) { 3189 p.fs.flags_fs |= DN_QSIZE_IS_BYTES; 3190 } 3191 ac--; av++; 3192 break; 3193 3194 case TOK_BUCKETS: 3195 NEED1("buckets needs argument\n"); 3196 p.fs.rq_size = strtoul(av[0], NULL, 0); 3197 ac--; av++; 3198 break; 3199 3200 case TOK_MASK: 3201 NEED1("mask needs mask specifier\n"); 3202 /* 3203 * per-flow queue, mask is dst_ip, dst_port, 3204 * src_ip, src_port, proto measured in bits 3205 */ 3206 par = NULL; 3207 3208 bzero(&p.fs.flow_mask, sizeof(p.fs.flow_mask)); 3209 end = NULL; 3210 3211 while (ac >= 1) { 3212 uint32_t *p32 = NULL; 3213 uint16_t *p16 = NULL; 3214 uint32_t *p20 = NULL; 3215 struct in6_addr *pa6 = NULL; 3216 uint32_t a; 3217 3218 tok = match_token(dummynet_params, *av); 3219 ac--; av++; 3220 switch(tok) { 3221 case TOK_ALL: 3222 /* 3223 * special case, all bits significant 3224 */ 3225 p.fs.flow_mask.dst_ip = ~0; 3226 p.fs.flow_mask.src_ip = ~0; 3227 p.fs.flow_mask.dst_port = ~0; 3228 p.fs.flow_mask.src_port = ~0; 3229 p.fs.flow_mask.proto = ~0; 3230 n2mask(&(p.fs.flow_mask.dst_ip6), 128); 3231 n2mask(&(p.fs.flow_mask.src_ip6), 128); 3232 p.fs.flow_mask.flow_id6 = ~0; 3233 p.fs.flags_fs |= DN_HAVE_FLOW_MASK; 3234 goto end_mask; 3235 3236 case TOK_DSTIP: 3237 p32 = &p.fs.flow_mask.dst_ip; 3238 break; 3239 3240 case TOK_SRCIP: 3241 p32 = &p.fs.flow_mask.src_ip; 3242 break; 3243 3244 case TOK_DSTIP6: 3245 pa6 = &(p.fs.flow_mask.dst_ip6); 3246 break; 3247 3248 case TOK_SRCIP6: 3249 pa6 = &(p.fs.flow_mask.src_ip6); 3250 break; 3251 3252 case TOK_FLOWID: 3253 p20 = &p.fs.flow_mask.flow_id6; 3254 break; 3255 3256 case TOK_DSTPORT: 3257 p16 = &p.fs.flow_mask.dst_port; 3258 break; 3259 3260 case TOK_SRCPORT: 3261 p16 = &p.fs.flow_mask.src_port; 3262 break; 3263 3264 case TOK_PROTO: 3265 break; 3266 3267 default: 3268 ac++; av--; /* backtrack */ 3269 goto end_mask; 3270 } 3271 if (ac < 1) 3272 errx(EX_USAGE, "mask: value missing"); 3273 if (*av[0] == '/') { 3274 a = strtoul(av[0]+1, &end, 0); 3275 if (pa6 == NULL) 3276 a = (a == 32) ? ~0 : (1 << a) - 1; 3277 } else 3278 a = strtoul(av[0], &end, 0); 3279 if (p32 != NULL) 3280 *p32 = a; 3281 else if (p16 != NULL) { 3282 if (a > 0xFFFF) 3283 errx(EX_DATAERR, 3284 "port mask must be 16 bit"); 3285 *p16 = (uint16_t)a; 3286 } else if (p20 != NULL) { 3287 if (a > 0xfffff) 3288 errx(EX_DATAERR, 3289 "flow_id mask must be 20 bit"); 3290 *p20 = (uint32_t)a; 3291 } else if (pa6 != NULL) { 3292 if (a < 0 || a > 128) 3293 errx(EX_DATAERR, 3294 "in6addr invalid mask len"); 3295 else 3296 n2mask(pa6, a); 3297 } else { 3298 if (a > 0xFF) 3299 errx(EX_DATAERR, 3300 "proto mask must be 8 bit"); 3301 p.fs.flow_mask.proto = (uint8_t)a; 3302 } 3303 if (a != 0) 3304 p.fs.flags_fs |= DN_HAVE_FLOW_MASK; 3305 ac--; av++; 3306 } /* end while, config masks */ 3307end_mask: 3308 break; 3309 3310 case TOK_RED: 3311 case TOK_GRED: 3312 NEED1("red/gred needs w_q/min_th/max_th/max_p\n"); 3313 p.fs.flags_fs |= DN_IS_RED; 3314 if (tok == TOK_GRED) 3315 p.fs.flags_fs |= DN_IS_GENTLE_RED; 3316 /* 3317 * the format for parameters is w_q/min_th/max_th/max_p 3318 */ 3319 if ((end = strsep(&av[0], "/"))) { 3320 double w_q = strtod(end, NULL); 3321 if (w_q > 1 || w_q <= 0) 3322 errx(EX_DATAERR, "0 < w_q <= 1"); 3323 p.fs.w_q = (int) (w_q * (1 << SCALE_RED)); 3324 } 3325 if ((end = strsep(&av[0], "/"))) { 3326 p.fs.min_th = strtoul(end, &end, 0); 3327 if (*end == 'K' || *end == 'k') 3328 p.fs.min_th *= 1024; 3329 } 3330 if ((end = strsep(&av[0], "/"))) { 3331 p.fs.max_th = strtoul(end, &end, 0); 3332 if (*end == 'K' || *end == 'k') 3333 p.fs.max_th *= 1024; 3334 } 3335 if ((end = strsep(&av[0], "/"))) { 3336 double max_p = strtod(end, NULL); 3337 if (max_p > 1 || max_p <= 0) 3338 errx(EX_DATAERR, "0 < max_p <= 1"); 3339 p.fs.max_p = (int)(max_p * (1 << SCALE_RED)); 3340 } 3341 ac--; av++; 3342 break; 3343 3344 case TOK_DROPTAIL: 3345 p.fs.flags_fs &= ~(DN_IS_RED|DN_IS_GENTLE_RED); 3346 break; 3347 3348 case TOK_BW: 3349 NEED1("bw needs bandwidth or interface\n"); 3350 if (do_pipe != 1) 3351 errx(EX_DATAERR, "bandwidth only valid for pipes"); 3352 /* 3353 * set clocking interface or bandwidth value 3354 */ 3355 if (av[0][0] >= 'a' && av[0][0] <= 'z') { 3356 int l = sizeof(p.if_name)-1; 3357 /* interface name */ 3358 strncpy(p.if_name, av[0], l); 3359 p.if_name[l] = '\0'; 3360 p.bandwidth = 0; 3361 } else { 3362 p.if_name[0] = '\0'; 3363 p.bandwidth = strtoul(av[0], &end, 0); 3364 if (*end == 'K' || *end == 'k') { 3365 end++; 3366 p.bandwidth *= 1000; 3367 } else if (*end == 'M') { 3368 end++; 3369 p.bandwidth *= 1000000; 3370 } 3371 if (*end == 'B' || 3372 _substrcmp2(end, "by", "bytes") == 0) 3373 p.bandwidth *= 8; 3374 if (p.bandwidth < 0) 3375 errx(EX_DATAERR, "bandwidth too large"); 3376 } 3377 ac--; av++; 3378 break; 3379 3380 case TOK_DELAY: 3381 if (do_pipe != 1) 3382 errx(EX_DATAERR, "delay only valid for pipes"); 3383 NEED1("delay needs argument 0..10000ms\n"); 3384 p.delay = strtoul(av[0], NULL, 0); 3385 ac--; av++; 3386 break; 3387 3388 case TOK_WEIGHT: 3389 if (do_pipe == 1) 3390 errx(EX_DATAERR,"weight only valid for queues"); 3391 NEED1("weight needs argument 0..100\n"); 3392 p.fs.weight = strtoul(av[0], &end, 0); 3393 ac--; av++; 3394 break; 3395 3396 case TOK_PIPE: 3397 if (do_pipe == 1) 3398 errx(EX_DATAERR,"pipe only valid for queues"); 3399 NEED1("pipe needs pipe_number\n"); 3400 p.fs.parent_nr = strtoul(av[0], &end, 0); 3401 ac--; av++; 3402 break; 3403 3404 default: 3405 errx(EX_DATAERR, "unrecognised option ``%s''", av[-1]); 3406 } 3407 } 3408 if (do_pipe == 1) { 3409 if (p.pipe_nr == 0) 3410 errx(EX_DATAERR, "pipe_nr must be > 0"); 3411 if (p.delay > 10000) 3412 errx(EX_DATAERR, "delay must be < 10000"); 3413 } else { /* do_pipe == 2, queue */ 3414 if (p.fs.parent_nr == 0) 3415 errx(EX_DATAERR, "pipe must be > 0"); 3416 if (p.fs.weight >100) 3417 errx(EX_DATAERR, "weight must be <= 100"); 3418 } 3419 if (p.fs.flags_fs & DN_QSIZE_IS_BYTES) { 3420 if (p.fs.qsize > 1024*1024) 3421 errx(EX_DATAERR, "queue size must be < 1MB"); 3422 } else { 3423 if (p.fs.qsize > 100) 3424 errx(EX_DATAERR, "2 <= queue size <= 100"); 3425 } 3426 if (p.fs.flags_fs & DN_IS_RED) { 3427 size_t len; 3428 int lookup_depth, avg_pkt_size; 3429 double s, idle, weight, w_q; 3430 struct clockinfo ck; 3431 int t; 3432 3433 if (p.fs.min_th >= p.fs.max_th) 3434 errx(EX_DATAERR, "min_th %d must be < than max_th %d", 3435 p.fs.min_th, p.fs.max_th); 3436 if (p.fs.max_th == 0) 3437 errx(EX_DATAERR, "max_th must be > 0"); 3438 3439 len = sizeof(int); 3440 if (sysctlbyname("net.inet.ip.dummynet.red_lookup_depth", 3441 &lookup_depth, &len, NULL, 0) == -1) 3442 3443 errx(1, "sysctlbyname(\"%s\")", 3444 "net.inet.ip.dummynet.red_lookup_depth"); 3445 if (lookup_depth == 0) 3446 errx(EX_DATAERR, "net.inet.ip.dummynet.red_lookup_depth" 3447 " must be greater than zero"); 3448 3449 len = sizeof(int); 3450 if (sysctlbyname("net.inet.ip.dummynet.red_avg_pkt_size", 3451 &avg_pkt_size, &len, NULL, 0) == -1) 3452 3453 errx(1, "sysctlbyname(\"%s\")", 3454 "net.inet.ip.dummynet.red_avg_pkt_size"); 3455 if (avg_pkt_size == 0) 3456 errx(EX_DATAERR, 3457 "net.inet.ip.dummynet.red_avg_pkt_size must" 3458 " be greater than zero"); 3459 3460 len = sizeof(struct clockinfo); 3461 if (sysctlbyname("kern.clockrate", &ck, &len, NULL, 0) == -1) 3462 errx(1, "sysctlbyname(\"%s\")", "kern.clockrate"); 3463 3464 /* 3465 * Ticks needed for sending a medium-sized packet. 3466 * Unfortunately, when we are configuring a WF2Q+ queue, we 3467 * do not have bandwidth information, because that is stored 3468 * in the parent pipe, and also we have multiple queues 3469 * competing for it. So we set s=0, which is not very 3470 * correct. But on the other hand, why do we want RED with 3471 * WF2Q+ ? 3472 */ 3473 if (p.bandwidth==0) /* this is a WF2Q+ queue */ 3474 s = 0; 3475 else 3476 s = ck.hz * avg_pkt_size * 8 / p.bandwidth; 3477 3478 /* 3479 * max idle time (in ticks) before avg queue size becomes 0. 3480 * NOTA: (3/w_q) is approx the value x so that 3481 * (1-w_q)^x < 10^-3. 3482 */ 3483 w_q = ((double)p.fs.w_q) / (1 << SCALE_RED); 3484 idle = s * 3. / w_q; 3485 p.fs.lookup_step = (int)idle / lookup_depth; 3486 if (!p.fs.lookup_step) 3487 p.fs.lookup_step = 1; 3488 weight = 1 - w_q; 3489 for (t = p.fs.lookup_step; t > 0; --t) 3490 weight *= weight; 3491 p.fs.lookup_weight = (int)(weight * (1 << SCALE_RED)); 3492 } 3493 i = do_cmd(IP_DUMMYNET_CONFIGURE, &p, sizeof p); 3494 if (i) 3495 err(1, "setsockopt(%s)", "IP_DUMMYNET_CONFIGURE"); 3496} 3497 3498static void 3499get_mac_addr_mask(char *p, uint8_t *addr, uint8_t *mask) 3500{ 3501 int i, l; 3502 3503 for (i=0; i<6; i++) 3504 addr[i] = mask[i] = 0; 3505 if (strcmp(p, "any") == 0) 3506 return; 3507 3508 for (i=0; *p && i<6;i++, p++) { 3509 addr[i] = strtol(p, &p, 16); 3510 if (*p != ':') /* we start with the mask */ 3511 break; 3512 } 3513 if (*p == '/') { /* mask len */ 3514 l = strtol(p+1, &p, 0); 3515 for (i=0; l>0; l -=8, i++) 3516 mask[i] = (l >=8) ? 0xff : (~0) << (8-l); 3517 } else if (*p == '&') { /* mask */ 3518 for (i=0, p++; *p && i<6;i++, p++) { 3519 mask[i] = strtol(p, &p, 16); 3520 if (*p != ':') 3521 break; 3522 } 3523 } else if (*p == '\0') { 3524 for (i=0; i<6; i++) 3525 mask[i] = 0xff; 3526 } 3527 for (i=0; i<6; i++) 3528 addr[i] &= mask[i]; 3529} 3530 3531/* 3532 * helper function, updates the pointer to cmd with the length 3533 * of the current command, and also cleans up the first word of 3534 * the new command in case it has been clobbered before. 3535 */ 3536static ipfw_insn * 3537next_cmd(ipfw_insn *cmd) 3538{ 3539 cmd += F_LEN(cmd); 3540 bzero(cmd, sizeof(*cmd)); 3541 return cmd; 3542} 3543 3544/* 3545 * Takes arguments and copies them into a comment 3546 */ 3547static void 3548fill_comment(ipfw_insn *cmd, int ac, char **av) 3549{ 3550 int i, l; 3551 char *p = (char *)(cmd + 1); 3552 3553 cmd->opcode = O_NOP; 3554 cmd->len = (cmd->len & (F_NOT | F_OR)); 3555 3556 /* Compute length of comment string. */ 3557 for (i = 0, l = 0; i < ac; i++) 3558 l += strlen(av[i]) + 1; 3559 if (l == 0) 3560 return; 3561 if (l > 84) 3562 errx(EX_DATAERR, 3563 "comment too long (max 80 chars)"); 3564 l = 1 + (l+3)/4; 3565 cmd->len = (cmd->len & (F_NOT | F_OR)) | l; 3566 for (i = 0; i < ac; i++) { 3567 strcpy(p, av[i]); 3568 p += strlen(av[i]); 3569 *p++ = ' '; 3570 } 3571 *(--p) = '\0'; 3572} 3573 3574/* 3575 * A function to fill simple commands of size 1. 3576 * Existing flags are preserved. 3577 */ 3578static void 3579fill_cmd(ipfw_insn *cmd, enum ipfw_opcodes opcode, int flags, uint16_t arg) 3580{ 3581 cmd->opcode = opcode; 3582 cmd->len = ((cmd->len | flags) & (F_NOT | F_OR)) | 1; 3583 cmd->arg1 = arg; 3584} 3585 3586/* 3587 * Fetch and add the MAC address and type, with masks. This generates one or 3588 * two microinstructions, and returns the pointer to the last one. 3589 */ 3590static ipfw_insn * 3591add_mac(ipfw_insn *cmd, int ac, char *av[]) 3592{ 3593 ipfw_insn_mac *mac; 3594 3595 if (ac < 2) 3596 errx(EX_DATAERR, "MAC dst src"); 3597 3598 cmd->opcode = O_MACADDR2; 3599 cmd->len = (cmd->len & (F_NOT | F_OR)) | F_INSN_SIZE(ipfw_insn_mac); 3600 3601 mac = (ipfw_insn_mac *)cmd; 3602 get_mac_addr_mask(av[0], mac->addr, mac->mask); /* dst */ 3603 get_mac_addr_mask(av[1], &(mac->addr[6]), &(mac->mask[6])); /* src */ 3604 return cmd; 3605} 3606 3607static ipfw_insn * 3608add_mactype(ipfw_insn *cmd, int ac, char *av) 3609{ 3610 if (ac < 1) 3611 errx(EX_DATAERR, "missing MAC type"); 3612 if (strcmp(av, "any") != 0) { /* we have a non-null type */ 3613 fill_newports((ipfw_insn_u16 *)cmd, av, IPPROTO_ETHERTYPE); 3614 cmd->opcode = O_MAC_TYPE; 3615 return cmd; 3616 } else 3617 return NULL; 3618} 3619 3620static ipfw_insn * 3621add_proto0(ipfw_insn *cmd, char *av, u_char *protop) 3622{ 3623 struct protoent *pe; 3624 char *ep; 3625 int proto; 3626 3627 proto = strtol(av, &ep, 10); 3628 if (*ep != '\0' || proto <= 0) { 3629 if ((pe = getprotobyname(av)) == NULL) 3630 return NULL; 3631 proto = pe->p_proto; 3632 } 3633 3634 fill_cmd(cmd, O_PROTO, 0, proto); 3635 *protop = proto; 3636 return cmd; 3637} 3638 3639static ipfw_insn * 3640add_proto(ipfw_insn *cmd, char *av, u_char *protop) 3641{ 3642 u_char proto = IPPROTO_IP; 3643 3644 if (_substrcmp(av, "all") == 0 || strcmp(av, "ip") == 0) 3645 ; /* do not set O_IP4 nor O_IP6 */ 3646 else if (strcmp(av, "ip4") == 0) 3647 /* explicit "just IPv4" rule */ 3648 fill_cmd(cmd, O_IP4, 0, 0); 3649 else if (strcmp(av, "ip6") == 0) { 3650 /* explicit "just IPv6" rule */ 3651 proto = IPPROTO_IPV6; 3652 fill_cmd(cmd, O_IP6, 0, 0); 3653 } else 3654 return add_proto0(cmd, av, protop); 3655 3656 *protop = proto; 3657 return cmd; 3658} 3659 3660static ipfw_insn * 3661add_proto_compat(ipfw_insn *cmd, char *av, u_char *protop) 3662{ 3663 u_char proto = IPPROTO_IP; 3664 3665 if (_substrcmp(av, "all") == 0 || strcmp(av, "ip") == 0) 3666 ; /* do not set O_IP4 nor O_IP6 */ 3667 else if (strcmp(av, "ipv4") == 0 || strcmp(av, "ip4") == 0) 3668 /* explicit "just IPv4" rule */ 3669 fill_cmd(cmd, O_IP4, 0, 0); 3670 else if (strcmp(av, "ipv6") == 0 || strcmp(av, "ip6") == 0) { 3671 /* explicit "just IPv6" rule */ 3672 proto = IPPROTO_IPV6; 3673 fill_cmd(cmd, O_IP6, 0, 0); 3674 } else 3675 return add_proto0(cmd, av, protop); 3676 3677 *protop = proto; 3678 return cmd; 3679} 3680 3681static ipfw_insn * 3682add_srcip(ipfw_insn *cmd, char *av) 3683{ 3684 fill_ip((ipfw_insn_ip *)cmd, av); 3685 if (cmd->opcode == O_IP_DST_SET) /* set */ 3686 cmd->opcode = O_IP_SRC_SET; 3687 else if (cmd->opcode == O_IP_DST_LOOKUP) /* table */ 3688 cmd->opcode = O_IP_SRC_LOOKUP; 3689 else if (F_LEN(cmd) == F_INSN_SIZE(ipfw_insn)) /* me */ 3690 cmd->opcode = O_IP_SRC_ME; 3691 else if (F_LEN(cmd) == F_INSN_SIZE(ipfw_insn_u32)) /* one IP */ 3692 cmd->opcode = O_IP_SRC; 3693 else /* addr/mask */ 3694 cmd->opcode = O_IP_SRC_MASK; 3695 return cmd; 3696} 3697 3698static ipfw_insn * 3699add_dstip(ipfw_insn *cmd, char *av) 3700{ 3701 fill_ip((ipfw_insn_ip *)cmd, av); 3702 if (cmd->opcode == O_IP_DST_SET) /* set */ 3703 ; 3704 else if (cmd->opcode == O_IP_DST_LOOKUP) /* table */ 3705 ; 3706 else if (F_LEN(cmd) == F_INSN_SIZE(ipfw_insn)) /* me */ 3707 cmd->opcode = O_IP_DST_ME; 3708 else if (F_LEN(cmd) == F_INSN_SIZE(ipfw_insn_u32)) /* one IP */ 3709 cmd->opcode = O_IP_DST; 3710 else /* addr/mask */ 3711 cmd->opcode = O_IP_DST_MASK; 3712 return cmd; 3713} 3714 3715static ipfw_insn * 3716add_ports(ipfw_insn *cmd, char *av, u_char proto, int opcode) 3717{ 3718 if (_substrcmp(av, "any") == 0) { 3719 return NULL; 3720 } else if (fill_newports((ipfw_insn_u16 *)cmd, av, proto)) { 3721 /* XXX todo: check that we have a protocol with ports */ 3722 cmd->opcode = opcode; 3723 return cmd; 3724 } 3725 return NULL; 3726} 3727 3728static ipfw_insn * 3729add_src(ipfw_insn *cmd, char *av, u_char proto) 3730{ 3731 struct in6_addr a; 3732 char *host, *ch; 3733 ipfw_insn *ret = NULL; 3734 3735 if ((host = strdup(av)) == NULL) 3736 return NULL; 3737 if ((ch = strrchr(host, '/')) != NULL) 3738 *ch = '\0'; 3739 3740 if (proto == IPPROTO_IPV6 || strcmp(av, "me6") == 0 || 3741 inet_pton(AF_INET6, host, &a)) 3742 ret = add_srcip6(cmd, av); 3743 /* XXX: should check for IPv4, not !IPv6 */ 3744 if ((ret == NULL) && proto == IPPROTO_IP || strcmp(av, "me") == 0 || 3745 !inet_pton(AF_INET6, host, &a)) 3746 ret = add_srcip(cmd, av); 3747 if ((ret == NULL) && strcmp(av, "any") != 0) 3748 ret = cmd; 3749 3750 free(host); 3751 return ret; 3752} 3753 3754static ipfw_insn * 3755add_dst(ipfw_insn *cmd, char *av, u_char proto) 3756{ 3757 struct in6_addr a; 3758 char *host, *ch; 3759 ipfw_insn *ret = NULL; 3760 3761 if ((host = strdup(av)) == NULL) 3762 return NULL; 3763 if ((ch = strrchr(host, '/')) != NULL) 3764 *ch = '\0'; 3765 3766 if (proto == IPPROTO_IPV6 || strcmp(av, "me6") == 0 || 3767 inet_pton(AF_INET6, host, &a)) 3768 ret = add_dstip6(cmd, av); 3769 /* XXX: should check for IPv4, not !IPv6 */ 3770 if ((ret == NULL) && proto == IPPROTO_IP || strcmp(av, "me") == 0 || 3771 !inet_pton(AF_INET6, av, &a)) 3772 ret = add_dstip(cmd, av); 3773 if ((ret == NULL) && strcmp(av, "any") != 0) 3774 ret = cmd; 3775 3776 free(host); 3777 return ret; 3778} 3779 3780/* 3781 * Parse arguments and assemble the microinstructions which make up a rule. 3782 * Rules are added into the 'rulebuf' and then copied in the correct order 3783 * into the actual rule. 3784 * 3785 * The syntax for a rule starts with the action, followed by 3786 * optional action parameters, and the various match patterns. 3787 * In the assembled microcode, the first opcode must be an O_PROBE_STATE 3788 * (generated if the rule includes a keep-state option), then the 3789 * various match patterns, log/altq actions, and the actual action. 3790 * 3791 */ 3792static void 3793add(int ac, char *av[]) 3794{ 3795 /* 3796 * rules are added into the 'rulebuf' and then copied in 3797 * the correct order into the actual rule. 3798 * Some things that need to go out of order (prob, action etc.) 3799 * go into actbuf[]. 3800 */ 3801 static uint32_t rulebuf[255], actbuf[255], cmdbuf[255]; 3802 3803 ipfw_insn *src, *dst, *cmd, *action, *prev=NULL; 3804 ipfw_insn *first_cmd; /* first match pattern */ 3805 3806 struct ip_fw *rule; 3807 3808 /* 3809 * various flags used to record that we entered some fields. 3810 */ 3811 ipfw_insn *have_state = NULL; /* check-state or keep-state */ 3812 ipfw_insn *have_log = NULL, *have_altq = NULL, *have_tag = NULL; 3813 size_t len; 3814 3815 int i; 3816 3817 int open_par = 0; /* open parenthesis ( */ 3818 3819 /* proto is here because it is used to fetch ports */ 3820 u_char proto = IPPROTO_IP; /* default protocol */ 3821 3822 double match_prob = 1; /* match probability, default is always match */ 3823 3824 bzero(actbuf, sizeof(actbuf)); /* actions go here */ 3825 bzero(cmdbuf, sizeof(cmdbuf)); 3826 bzero(rulebuf, sizeof(rulebuf)); 3827 3828 rule = (struct ip_fw *)rulebuf; 3829 cmd = (ipfw_insn *)cmdbuf; 3830 action = (ipfw_insn *)actbuf; 3831 3832 av++; ac--; 3833 3834 /* [rule N] -- Rule number optional */ 3835 if (ac && isdigit(**av)) { 3836 rule->rulenum = atoi(*av); 3837 av++; 3838 ac--; 3839 } 3840 3841 /* [set N] -- set number (0..RESVD_SET), optional */ 3842 if (ac > 1 && _substrcmp(*av, "set") == 0) { 3843 int set = strtoul(av[1], NULL, 10); 3844 if (set < 0 || set > RESVD_SET) 3845 errx(EX_DATAERR, "illegal set %s", av[1]); 3846 rule->set = set; 3847 av += 2; ac -= 2; 3848 } 3849 3850 /* [prob D] -- match probability, optional */ 3851 if (ac > 1 && _substrcmp(*av, "prob") == 0) { 3852 match_prob = strtod(av[1], NULL); 3853 3854 if (match_prob <= 0 || match_prob > 1) 3855 errx(EX_DATAERR, "illegal match prob. %s", av[1]); 3856 av += 2; ac -= 2; 3857 } 3858 3859 /* action -- mandatory */ 3860 NEED1("missing action"); 3861 i = match_token(rule_actions, *av); 3862 ac--; av++; 3863 action->len = 1; /* default */ 3864 switch(i) { 3865 case TOK_CHECKSTATE: 3866 have_state = action; 3867 action->opcode = O_CHECK_STATE; 3868 break; 3869 3870 case TOK_ACCEPT: 3871 action->opcode = O_ACCEPT; 3872 break; 3873 3874 case TOK_DENY: 3875 action->opcode = O_DENY; 3876 action->arg1 = 0; 3877 break; 3878 3879 case TOK_REJECT: 3880 action->opcode = O_REJECT; 3881 action->arg1 = ICMP_UNREACH_HOST; 3882 break; 3883 3884 case TOK_RESET: 3885 action->opcode = O_REJECT; 3886 action->arg1 = ICMP_REJECT_RST; 3887 break; 3888 3889 case TOK_RESET6: 3890 action->opcode = O_UNREACH6; 3891 action->arg1 = ICMP6_UNREACH_RST; 3892 break; 3893 3894 case TOK_UNREACH: 3895 action->opcode = O_REJECT; 3896 NEED1("missing reject code"); 3897 fill_reject_code(&action->arg1, *av); 3898 ac--; av++; 3899 break; 3900 3901 case TOK_UNREACH6: 3902 action->opcode = O_UNREACH6; 3903 NEED1("missing unreach code"); 3904 fill_unreach6_code(&action->arg1, *av); 3905 ac--; av++; 3906 break; 3907 3908 case TOK_COUNT: 3909 action->opcode = O_COUNT; 3910 break; 3911 3912 case TOK_QUEUE: 3913 action->opcode = O_QUEUE; 3914 goto chkarg; 3915 case TOK_PIPE: 3916 action->opcode = O_PIPE; 3917 goto chkarg; 3918 case TOK_SKIPTO: 3919 action->opcode = O_SKIPTO; 3920 goto chkarg; 3921 case TOK_NETGRAPH: 3922 action->opcode = O_NETGRAPH; 3923 goto chkarg; 3924 case TOK_NGTEE: 3925 action->opcode = O_NGTEE; 3926 goto chkarg; 3927 case TOK_DIVERT: 3928 action->opcode = O_DIVERT; 3929 goto chkarg; 3930 case TOK_TEE: 3931 action->opcode = O_TEE; 3932chkarg: 3933 if (!ac) 3934 errx(EX_USAGE, "missing argument for %s", *(av - 1)); 3935 if (isdigit(**av)) { 3936 action->arg1 = strtoul(*av, NULL, 10); 3937 if (action->arg1 <= 0 || action->arg1 >= IP_FW_TABLEARG) 3938 errx(EX_DATAERR, "illegal argument for %s", 3939 *(av - 1)); 3940 } else if (_substrcmp(*av, TABLEARG) == 0) { 3941 action->arg1 = IP_FW_TABLEARG; 3942 } else if (i == TOK_DIVERT || i == TOK_TEE) { 3943 struct servent *s; 3944 setservent(1); 3945 s = getservbyname(av[0], "divert"); 3946 if (s != NULL) 3947 action->arg1 = ntohs(s->s_port); 3948 else 3949 errx(EX_DATAERR, "illegal divert/tee port"); 3950 } else 3951 errx(EX_DATAERR, "illegal argument for %s", *(av - 1)); 3952 ac--; av++; 3953 break; 3954 3955 case TOK_FORWARD: { 3956 ipfw_insn_sa *p = (ipfw_insn_sa *)action; 3957 char *s, *end; 3958 3959 NEED1("missing forward address[:port]"); 3960 3961 action->opcode = O_FORWARD_IP; 3962 action->len = F_INSN_SIZE(ipfw_insn_sa); 3963 3964 p->sa.sin_len = sizeof(struct sockaddr_in); 3965 p->sa.sin_family = AF_INET; 3966 p->sa.sin_port = 0; 3967 /* 3968 * locate the address-port separator (':' or ',') 3969 */ 3970 s = strchr(*av, ':'); 3971 if (s == NULL) 3972 s = strchr(*av, ','); 3973 if (s != NULL) { 3974 *(s++) = '\0'; 3975 i = strtoport(s, &end, 0 /* base */, 0 /* proto */); 3976 if (s == end) 3977 errx(EX_DATAERR, 3978 "illegal forwarding port ``%s''", s); 3979 p->sa.sin_port = (u_short)i; 3980 } 3981 lookup_host(*av, &(p->sa.sin_addr)); 3982 } 3983 ac--; av++; 3984 break; 3985 3986 case TOK_COMMENT: 3987 /* pretend it is a 'count' rule followed by the comment */ 3988 action->opcode = O_COUNT; 3989 ac++; av--; /* go back... */ 3990 break; 3991 3992 default: 3993 errx(EX_DATAERR, "invalid action %s\n", av[-1]); 3994 } 3995 action = next_cmd(action); 3996 3997 /* 3998 * [altq queuename] -- altq tag, optional 3999 * [log [logamount N]] -- log, optional 4000 * 4001 * If they exist, it go first in the cmdbuf, but then it is 4002 * skipped in the copy section to the end of the buffer. 4003 */ 4004 while (ac != 0 && (i = match_token(rule_action_params, *av)) != -1) { 4005 ac--; av++; 4006 switch (i) { 4007 case TOK_LOG: 4008 { 4009 ipfw_insn_log *c = (ipfw_insn_log *)cmd; 4010 int l; 4011 4012 if (have_log) 4013 errx(EX_DATAERR, 4014 "log cannot be specified more than once"); 4015 have_log = (ipfw_insn *)c; 4016 cmd->len = F_INSN_SIZE(ipfw_insn_log); 4017 cmd->opcode = O_LOG; 4018 if (ac && _substrcmp(*av, "logamount") == 0) { 4019 ac--; av++; 4020 NEED1("logamount requires argument"); 4021 l = atoi(*av); 4022 if (l < 0) 4023 errx(EX_DATAERR, 4024 "logamount must be positive"); 4025 c->max_log = l; 4026 ac--; av++; 4027 } else { 4028 len = sizeof(c->max_log); 4029 if (sysctlbyname("net.inet.ip.fw.verbose_limit", 4030 &c->max_log, &len, NULL, 0) == -1) 4031 errx(1, "sysctlbyname(\"%s\")", 4032 "net.inet.ip.fw.verbose_limit"); 4033 } 4034 } 4035 break; 4036 4037 case TOK_ALTQ: 4038 { 4039 ipfw_insn_altq *a = (ipfw_insn_altq *)cmd; 4040 4041 NEED1("missing altq queue name"); 4042 if (have_altq) 4043 errx(EX_DATAERR, 4044 "altq cannot be specified more than once"); 4045 have_altq = (ipfw_insn *)a; 4046 cmd->len = F_INSN_SIZE(ipfw_insn_altq); 4047 cmd->opcode = O_ALTQ; 4048 fill_altq_qid(&a->qid, *av); 4049 ac--; av++; 4050 } 4051 break; 4052 4053 case TOK_TAG: 4054 case TOK_UNTAG: 4055 { 4056 if (have_tag) 4057 errx(EX_USAGE, "tag and untag cannot be specified more than once"); 4058 NEED1("missing tag number"); 4059 NOT_NUMBER(*av, "invalid tag number"); 4060 have_tag = cmd; 4061 fill_cmd(cmd, O_TAG, (i == TOK_TAG) ? 0: F_NOT, 4062 strtoul(*av, NULL, 0)); 4063 ac--; av++; 4064 } 4065 break; 4066 4067 default: 4068 abort(); 4069 } 4070 cmd = next_cmd(cmd); 4071 } 4072 4073 if (have_state) /* must be a check-state, we are done */ 4074 goto done; 4075 4076#define OR_START(target) \ 4077 if (ac && (*av[0] == '(' || *av[0] == '{')) { \ 4078 if (open_par) \ 4079 errx(EX_USAGE, "nested \"(\" not allowed\n"); \ 4080 prev = NULL; \ 4081 open_par = 1; \ 4082 if ( (av[0])[1] == '\0') { \ 4083 ac--; av++; \ 4084 } else \ 4085 (*av)++; \ 4086 } \ 4087 target: \ 4088 4089 4090#define CLOSE_PAR \ 4091 if (open_par) { \ 4092 if (ac && ( \ 4093 strcmp(*av, ")") == 0 || \ 4094 strcmp(*av, "}") == 0)) { \ 4095 prev = NULL; \ 4096 open_par = 0; \ 4097 ac--; av++; \ 4098 } else \ 4099 errx(EX_USAGE, "missing \")\"\n"); \ 4100 } 4101 4102#define NOT_BLOCK \ 4103 if (ac && _substrcmp(*av, "not") == 0) { \ 4104 if (cmd->len & F_NOT) \ 4105 errx(EX_USAGE, "double \"not\" not allowed\n"); \ 4106 cmd->len |= F_NOT; \ 4107 ac--; av++; \ 4108 } 4109 4110#define OR_BLOCK(target) \ 4111 if (ac && _substrcmp(*av, "or") == 0) { \ 4112 if (prev == NULL || open_par == 0) \ 4113 errx(EX_DATAERR, "invalid OR block"); \ 4114 prev->len |= F_OR; \ 4115 ac--; av++; \ 4116 goto target; \ 4117 } \ 4118 CLOSE_PAR; 4119 4120 first_cmd = cmd; 4121 4122#if 0 4123 /* 4124 * MAC addresses, optional. 4125 * If we have this, we skip the part "proto from src to dst" 4126 * and jump straight to the option parsing. 4127 */ 4128 NOT_BLOCK; 4129 NEED1("missing protocol"); 4130 if (_substrcmp(*av, "MAC") == 0 || 4131 _substrcmp(*av, "mac") == 0) { 4132 ac--; av++; /* the "MAC" keyword */ 4133 add_mac(cmd, ac, av); /* exits in case of errors */ 4134 cmd = next_cmd(cmd); 4135 ac -= 2; av += 2; /* dst-mac and src-mac */ 4136 NOT_BLOCK; 4137 NEED1("missing mac type"); 4138 if (add_mactype(cmd, ac, av[0])) 4139 cmd = next_cmd(cmd); 4140 ac--; av++; /* any or mac-type */ 4141 goto read_options; 4142 } 4143#endif 4144 4145 /* 4146 * protocol, mandatory 4147 */ 4148 OR_START(get_proto); 4149 NOT_BLOCK; 4150 NEED1("missing protocol"); 4151 if (add_proto_compat(cmd, *av, &proto)) { 4152 av++; ac--; 4153 if (F_LEN(cmd) != 0) { 4154 prev = cmd; 4155 cmd = next_cmd(cmd); 4156 } 4157 } else if (first_cmd != cmd) { 4158 errx(EX_DATAERR, "invalid protocol ``%s''", *av); 4159 } else 4160 goto read_options; 4161 OR_BLOCK(get_proto); 4162 4163 /* 4164 * "from", mandatory 4165 */ 4166 if (!ac || _substrcmp(*av, "from") != 0) 4167 errx(EX_USAGE, "missing ``from''"); 4168 ac--; av++; 4169 4170 /* 4171 * source IP, mandatory 4172 */ 4173 OR_START(source_ip); 4174 NOT_BLOCK; /* optional "not" */ 4175 NEED1("missing source address"); 4176 if (add_src(cmd, *av, proto)) { 4177 ac--; av++; 4178 if (F_LEN(cmd) != 0) { /* ! any */ 4179 prev = cmd; 4180 cmd = next_cmd(cmd); 4181 } 4182 } else 4183 errx(EX_USAGE, "bad source address %s", *av); 4184 OR_BLOCK(source_ip); 4185 4186 /* 4187 * source ports, optional 4188 */ 4189 NOT_BLOCK; /* optional "not" */ 4190 if (ac) { 4191 if (_substrcmp(*av, "any") == 0 || 4192 add_ports(cmd, *av, proto, O_IP_SRCPORT)) { 4193 ac--; av++; 4194 if (F_LEN(cmd) != 0) 4195 cmd = next_cmd(cmd); 4196 } 4197 } 4198 4199 /* 4200 * "to", mandatory 4201 */ 4202 if (!ac || _substrcmp(*av, "to") != 0) 4203 errx(EX_USAGE, "missing ``to''"); 4204 av++; ac--; 4205 4206 /* 4207 * destination, mandatory 4208 */ 4209 OR_START(dest_ip); 4210 NOT_BLOCK; /* optional "not" */ 4211 NEED1("missing dst address"); 4212 if (add_dst(cmd, *av, proto)) { 4213 ac--; av++; 4214 if (F_LEN(cmd) != 0) { /* ! any */ 4215 prev = cmd; 4216 cmd = next_cmd(cmd); 4217 } 4218 } else 4219 errx( EX_USAGE, "bad destination address %s", *av); 4220 OR_BLOCK(dest_ip); 4221 4222 /* 4223 * dest. ports, optional 4224 */ 4225 NOT_BLOCK; /* optional "not" */ 4226 if (ac) { 4227 if (_substrcmp(*av, "any") == 0 || 4228 add_ports(cmd, *av, proto, O_IP_DSTPORT)) { 4229 ac--; av++; 4230 if (F_LEN(cmd) != 0) 4231 cmd = next_cmd(cmd); 4232 } 4233 } 4234 4235read_options: 4236 if (ac && first_cmd == cmd) { 4237 /* 4238 * nothing specified so far, store in the rule to ease 4239 * printout later. 4240 */ 4241 rule->_pad = 1; 4242 } 4243 prev = NULL; 4244 while (ac) { 4245 char *s; 4246 ipfw_insn_u32 *cmd32; /* alias for cmd */ 4247 4248 s = *av; 4249 cmd32 = (ipfw_insn_u32 *)cmd; 4250 4251 if (*s == '!') { /* alternate syntax for NOT */ 4252 if (cmd->len & F_NOT) 4253 errx(EX_USAGE, "double \"not\" not allowed\n"); 4254 cmd->len = F_NOT; 4255 s++; 4256 } 4257 i = match_token(rule_options, s); 4258 ac--; av++; 4259 switch(i) { 4260 case TOK_NOT: 4261 if (cmd->len & F_NOT) 4262 errx(EX_USAGE, "double \"not\" not allowed\n"); 4263 cmd->len = F_NOT; 4264 break; 4265 4266 case TOK_OR: 4267 if (open_par == 0 || prev == NULL) 4268 errx(EX_USAGE, "invalid \"or\" block\n"); 4269 prev->len |= F_OR; 4270 break; 4271 4272 case TOK_STARTBRACE: 4273 if (open_par) 4274 errx(EX_USAGE, "+nested \"(\" not allowed\n"); 4275 open_par = 1; 4276 break; 4277 4278 case TOK_ENDBRACE: 4279 if (!open_par) 4280 errx(EX_USAGE, "+missing \")\"\n"); 4281 open_par = 0; 4282 prev = NULL; 4283 break; 4284 4285 case TOK_IN: 4286 fill_cmd(cmd, O_IN, 0, 0); 4287 break; 4288 4289 case TOK_OUT: 4290 cmd->len ^= F_NOT; /* toggle F_NOT */ 4291 fill_cmd(cmd, O_IN, 0, 0); 4292 break; 4293 4294 case TOK_DIVERTED: 4295 fill_cmd(cmd, O_DIVERTED, 0, 3); 4296 break; 4297 4298 case TOK_DIVERTEDLOOPBACK: 4299 fill_cmd(cmd, O_DIVERTED, 0, 1); 4300 break; 4301 4302 case TOK_DIVERTEDOUTPUT: 4303 fill_cmd(cmd, O_DIVERTED, 0, 2); 4304 break; 4305 4306 case TOK_FRAG: 4307 fill_cmd(cmd, O_FRAG, 0, 0); 4308 break; 4309 4310 case TOK_LAYER2: 4311 fill_cmd(cmd, O_LAYER2, 0, 0); 4312 break; 4313 4314 case TOK_XMIT: 4315 case TOK_RECV: 4316 case TOK_VIA: 4317 NEED1("recv, xmit, via require interface name" 4318 " or address"); 4319 fill_iface((ipfw_insn_if *)cmd, av[0]); 4320 ac--; av++; 4321 if (F_LEN(cmd) == 0) /* not a valid address */ 4322 break; 4323 if (i == TOK_XMIT) 4324 cmd->opcode = O_XMIT; 4325 else if (i == TOK_RECV) 4326 cmd->opcode = O_RECV; 4327 else if (i == TOK_VIA) 4328 cmd->opcode = O_VIA; 4329 break; 4330 4331 case TOK_ICMPTYPES: 4332 NEED1("icmptypes requires list of types"); 4333 fill_icmptypes((ipfw_insn_u32 *)cmd, *av); 4334 av++; ac--; 4335 break; 4336 4337 case TOK_ICMP6TYPES: 4338 NEED1("icmptypes requires list of types"); 4339 fill_icmp6types((ipfw_insn_icmp6 *)cmd, *av); 4340 av++; ac--; 4341 break; 4342 4343 case TOK_IPTTL: 4344 NEED1("ipttl requires TTL"); 4345 if (strpbrk(*av, "-,")) { 4346 if (!add_ports(cmd, *av, 0, O_IPTTL)) 4347 errx(EX_DATAERR, "invalid ipttl %s", *av); 4348 } else 4349 fill_cmd(cmd, O_IPTTL, 0, strtoul(*av, NULL, 0)); 4350 ac--; av++; 4351 break; 4352 4353 case TOK_IPID: 4354 NEED1("ipid requires id"); 4355 if (strpbrk(*av, "-,")) { 4356 if (!add_ports(cmd, *av, 0, O_IPID)) 4357 errx(EX_DATAERR, "invalid ipid %s", *av); 4358 } else 4359 fill_cmd(cmd, O_IPID, 0, strtoul(*av, NULL, 0)); 4360 ac--; av++; 4361 break; 4362 4363 case TOK_IPLEN: 4364 NEED1("iplen requires length"); 4365 if (strpbrk(*av, "-,")) { 4366 if (!add_ports(cmd, *av, 0, O_IPLEN)) 4367 errx(EX_DATAERR, "invalid ip len %s", *av); 4368 } else 4369 fill_cmd(cmd, O_IPLEN, 0, strtoul(*av, NULL, 0)); 4370 ac--; av++; 4371 break; 4372 4373 case TOK_IPVER: 4374 NEED1("ipver requires version"); 4375 fill_cmd(cmd, O_IPVER, 0, strtoul(*av, NULL, 0)); 4376 ac--; av++; 4377 break; 4378 4379 case TOK_IPPRECEDENCE: 4380 NEED1("ipprecedence requires value"); 4381 fill_cmd(cmd, O_IPPRECEDENCE, 0, 4382 (strtoul(*av, NULL, 0) & 7) << 5); 4383 ac--; av++; 4384 break; 4385 4386 case TOK_IPOPTS: 4387 NEED1("missing argument for ipoptions"); 4388 fill_flags(cmd, O_IPOPT, f_ipopts, *av); 4389 ac--; av++; 4390 break; 4391 4392 case TOK_IPTOS: 4393 NEED1("missing argument for iptos"); 4394 fill_flags(cmd, O_IPTOS, f_iptos, *av); 4395 ac--; av++; 4396 break; 4397 4398 case TOK_UID: 4399 NEED1("uid requires argument"); 4400 { 4401 char *end; 4402 uid_t uid; 4403 struct passwd *pwd; 4404 4405 cmd->opcode = O_UID; 4406 uid = strtoul(*av, &end, 0); 4407 pwd = (*end == '\0') ? getpwuid(uid) : getpwnam(*av); 4408 if (pwd == NULL) 4409 errx(EX_DATAERR, "uid \"%s\" nonexistent", *av); 4410 cmd32->d[0] = pwd->pw_uid; 4411 cmd->len |= F_INSN_SIZE(ipfw_insn_u32); 4412 ac--; av++; 4413 } 4414 break; 4415 4416 case TOK_GID: 4417 NEED1("gid requires argument"); 4418 { 4419 char *end; 4420 gid_t gid; 4421 struct group *grp; 4422 4423 cmd->opcode = O_GID; 4424 gid = strtoul(*av, &end, 0); 4425 grp = (*end == '\0') ? getgrgid(gid) : getgrnam(*av); 4426 if (grp == NULL) 4427 errx(EX_DATAERR, "gid \"%s\" nonexistent", *av); 4428 cmd32->d[0] = grp->gr_gid; 4429 cmd->len |= F_INSN_SIZE(ipfw_insn_u32); 4430 ac--; av++; 4431 } 4432 break; 4433 4434 case TOK_JAIL: 4435 NEED1("jail requires argument"); 4436 { 4437 char *end; 4438 int jid; 4439 4440 cmd->opcode = O_JAIL; 4441 jid = (int)strtol(*av, &end, 0); 4442 if (jid < 0 || *end != '\0') 4443 errx(EX_DATAERR, "jail requires prison ID"); 4444 cmd32->d[0] = (uint32_t)jid; 4445 cmd->len |= F_INSN_SIZE(ipfw_insn_u32); 4446 ac--; av++; 4447 } 4448 break; 4449 4450 case TOK_ESTAB: 4451 fill_cmd(cmd, O_ESTAB, 0, 0); 4452 break; 4453 4454 case TOK_SETUP: 4455 fill_cmd(cmd, O_TCPFLAGS, 0, 4456 (TH_SYN) | ( (TH_ACK) & 0xff) <<8 ); 4457 break; 4458 4459 case TOK_TCPDATALEN: 4460 NEED1("tcpdatalen requires length"); 4461 if (strpbrk(*av, "-,")) { 4462 if (!add_ports(cmd, *av, 0, O_TCPDATALEN)) 4463 errx(EX_DATAERR, "invalid tcpdata len %s", *av); 4464 } else 4465 fill_cmd(cmd, O_TCPDATALEN, 0, 4466 strtoul(*av, NULL, 0)); 4467 ac--; av++; 4468 break; 4469 4470 case TOK_TCPOPTS: 4471 NEED1("missing argument for tcpoptions"); 4472 fill_flags(cmd, O_TCPOPTS, f_tcpopts, *av); 4473 ac--; av++; 4474 break; 4475 4476 case TOK_TCPSEQ: 4477 case TOK_TCPACK: 4478 NEED1("tcpseq/tcpack requires argument"); 4479 cmd->len = F_INSN_SIZE(ipfw_insn_u32); 4480 cmd->opcode = (i == TOK_TCPSEQ) ? O_TCPSEQ : O_TCPACK; 4481 cmd32->d[0] = htonl(strtoul(*av, NULL, 0)); 4482 ac--; av++; 4483 break; 4484 4485 case TOK_TCPWIN: 4486 NEED1("tcpwin requires length"); 4487 fill_cmd(cmd, O_TCPWIN, 0, 4488 htons(strtoul(*av, NULL, 0))); 4489 ac--; av++; 4490 break; 4491 4492 case TOK_TCPFLAGS: 4493 NEED1("missing argument for tcpflags"); 4494 cmd->opcode = O_TCPFLAGS; 4495 fill_flags(cmd, O_TCPFLAGS, f_tcpflags, *av); 4496 ac--; av++; 4497 break; 4498 4499 case TOK_KEEPSTATE: 4500 if (open_par) 4501 errx(EX_USAGE, "keep-state cannot be part " 4502 "of an or block"); 4503 if (have_state) 4504 errx(EX_USAGE, "only one of keep-state " 4505 "and limit is allowed"); 4506 have_state = cmd; 4507 fill_cmd(cmd, O_KEEP_STATE, 0, 0); 4508 break; 4509 4510 case TOK_LIMIT: 4511 if (open_par) 4512 errx(EX_USAGE, "limit cannot be part " 4513 "of an or block"); 4514 if (have_state) 4515 errx(EX_USAGE, "only one of keep-state " 4516 "and limit is allowed"); 4517 NEED1("limit needs mask and # of connections"); 4518 have_state = cmd; 4519 { 4520 ipfw_insn_limit *c = (ipfw_insn_limit *)cmd; 4521 4522 cmd->len = F_INSN_SIZE(ipfw_insn_limit); 4523 cmd->opcode = O_LIMIT; 4524 c->limit_mask = 0; 4525 c->conn_limit = 0; 4526 for (; ac >1 ;) { 4527 int val; 4528 4529 val = match_token(limit_masks, *av); 4530 if (val <= 0) 4531 break; 4532 c->limit_mask |= val; 4533 ac--; av++; 4534 } 4535 c->conn_limit = atoi(*av); 4536 if (c->conn_limit == 0) 4537 errx(EX_USAGE, "limit: limit must be >0"); 4538 if (c->limit_mask == 0) 4539 errx(EX_USAGE, "missing limit mask"); 4540 ac--; av++; 4541 } 4542 break; 4543 4544 case TOK_PROTO: 4545 NEED1("missing protocol"); 4546 if (add_proto(cmd, *av, &proto)) { 4547 ac--; av++; 4548 } else 4549 errx(EX_DATAERR, "invalid protocol ``%s''", 4550 *av); 4551 break; 4552 4553 case TOK_SRCIP: 4554 NEED1("missing source IP"); 4555 if (add_srcip(cmd, *av)) { 4556 ac--; av++; 4557 } 4558 break; 4559 4560 case TOK_DSTIP: 4561 NEED1("missing destination IP"); 4562 if (add_dstip(cmd, *av)) { 4563 ac--; av++; 4564 } 4565 break; 4566 4567 case TOK_SRCIP6: 4568 NEED1("missing source IP6"); 4569 if (add_srcip6(cmd, *av)) { 4570 ac--; av++; 4571 } 4572 break; 4573 4574 case TOK_DSTIP6: 4575 NEED1("missing destination IP6"); 4576 if (add_dstip6(cmd, *av)) { 4577 ac--; av++; 4578 } 4579 break; 4580 4581 case TOK_SRCPORT: 4582 NEED1("missing source port"); 4583 if (_substrcmp(*av, "any") == 0 || 4584 add_ports(cmd, *av, proto, O_IP_SRCPORT)) { 4585 ac--; av++; 4586 } else 4587 errx(EX_DATAERR, "invalid source port %s", *av); 4588 break; 4589 4590 case TOK_DSTPORT: 4591 NEED1("missing destination port"); 4592 if (_substrcmp(*av, "any") == 0 || 4593 add_ports(cmd, *av, proto, O_IP_DSTPORT)) { 4594 ac--; av++; 4595 } else 4596 errx(EX_DATAERR, "invalid destination port %s", 4597 *av); 4598 break; 4599 4600 case TOK_MAC: 4601 if (add_mac(cmd, ac, av)) { 4602 ac -= 2; av += 2; 4603 } 4604 break; 4605 4606 case TOK_MACTYPE: 4607 NEED1("missing mac type"); 4608 if (!add_mactype(cmd, ac, *av)) 4609 errx(EX_DATAERR, "invalid mac type %s", *av); 4610 ac--; av++; 4611 break; 4612 4613 case TOK_VERREVPATH: 4614 fill_cmd(cmd, O_VERREVPATH, 0, 0); 4615 break; 4616 4617 case TOK_VERSRCREACH: 4618 fill_cmd(cmd, O_VERSRCREACH, 0, 0); 4619 break; 4620 4621 case TOK_ANTISPOOF: 4622 fill_cmd(cmd, O_ANTISPOOF, 0, 0); 4623 break; 4624 4625 case TOK_IPSEC: 4626 fill_cmd(cmd, O_IPSEC, 0, 0); 4627 break; 4628 4629 case TOK_IPV6: 4630 fill_cmd(cmd, O_IP6, 0, 0); 4631 break; 4632 4633 case TOK_IPV4: 4634 fill_cmd(cmd, O_IP4, 0, 0); 4635 break; 4636 4637 case TOK_EXT6HDR: 4638 fill_ext6hdr( cmd, *av ); 4639 ac--; av++; 4640 break; 4641 4642 case TOK_FLOWID: 4643 if (proto != IPPROTO_IPV6 ) 4644 errx( EX_USAGE, "flow-id filter is active " 4645 "only for ipv6 protocol\n"); 4646 fill_flow6( (ipfw_insn_u32 *) cmd, *av ); 4647 ac--; av++; 4648 break; 4649 4650 case TOK_COMMENT: 4651 fill_comment(cmd, ac, av); 4652 av += ac; 4653 ac = 0; 4654 break; 4655 4656 case TOK_TAGGED: 4657 NEED1("missing tag number"); 4658 if (strpbrk(*av, "-,")) { 4659 if (!add_ports(cmd, *av, 0, O_TAGGED)) 4660 errx(EX_DATAERR, "invalid tag %s", *av); 4661 } else { 4662 NOT_NUMBER(*av, "invalid tag number"); 4663 fill_cmd(cmd, O_TAGGED, 0, 4664 strtoul(*av, NULL, 0)); 4665 } 4666 ac--; av++; 4667 break; 4668 4669 default: 4670 errx(EX_USAGE, "unrecognised option [%d] %s\n", i, s); 4671 } 4672 if (F_LEN(cmd) > 0) { /* prepare to advance */ 4673 prev = cmd; 4674 cmd = next_cmd(cmd); 4675 } 4676 } 4677 4678done: 4679 /* 4680 * Now copy stuff into the rule. 4681 * If we have a keep-state option, the first instruction 4682 * must be a PROBE_STATE (which is generated here). 4683 * If we have a LOG option, it was stored as the first command, 4684 * and now must be moved to the top of the action part. 4685 */ 4686 dst = (ipfw_insn *)rule->cmd; 4687 4688 /* 4689 * First thing to write into the command stream is the match probability. 4690 */ 4691 if (match_prob != 1) { /* 1 means always match */ 4692 dst->opcode = O_PROB; 4693 dst->len = 2; 4694 *((int32_t *)(dst+1)) = (int32_t)(match_prob * 0x7fffffff); 4695 dst += dst->len; 4696 } 4697 4698 /* 4699 * generate O_PROBE_STATE if necessary 4700 */ 4701 if (have_state && have_state->opcode != O_CHECK_STATE) { 4702 fill_cmd(dst, O_PROBE_STATE, 0, 0); 4703 dst = next_cmd(dst); 4704 } 4705 4706 /* copy all commands but O_LOG, O_KEEP_STATE, O_LIMIT, O_ALTQ, O_TAG */ 4707 for (src = (ipfw_insn *)cmdbuf; src != cmd; src += i) { 4708 i = F_LEN(src); 4709 4710 switch (src->opcode) { 4711 case O_LOG: 4712 case O_KEEP_STATE: 4713 case O_LIMIT: 4714 case O_ALTQ: 4715 case O_TAG: 4716 break; 4717 default: 4718 bcopy(src, dst, i * sizeof(uint32_t)); 4719 dst += i; 4720 } 4721 } 4722 4723 /* 4724 * put back the have_state command as last opcode 4725 */ 4726 if (have_state && have_state->opcode != O_CHECK_STATE) { 4727 i = F_LEN(have_state); 4728 bcopy(have_state, dst, i * sizeof(uint32_t)); 4729 dst += i; 4730 } 4731 /* 4732 * start action section 4733 */ 4734 rule->act_ofs = dst - rule->cmd; 4735 4736 /* put back O_LOG, O_ALTQ, O_TAG if necessary */ 4737 if (have_log) { 4738 i = F_LEN(have_log); 4739 bcopy(have_log, dst, i * sizeof(uint32_t)); 4740 dst += i; 4741 } 4742 if (have_altq) { 4743 i = F_LEN(have_altq); 4744 bcopy(have_altq, dst, i * sizeof(uint32_t)); 4745 dst += i; 4746 } 4747 if (have_tag) { 4748 i = F_LEN(have_tag); 4749 bcopy(have_tag, dst, i * sizeof(uint32_t)); 4750 dst += i; 4751 } 4752 /* 4753 * copy all other actions 4754 */ 4755 for (src = (ipfw_insn *)actbuf; src != action; src += i) { 4756 i = F_LEN(src); 4757 bcopy(src, dst, i * sizeof(uint32_t)); 4758 dst += i; 4759 } 4760 4761 rule->cmd_len = (uint32_t *)dst - (uint32_t *)(rule->cmd); 4762 i = (char *)dst - (char *)rule; 4763 if (do_cmd(IP_FW_ADD, rule, (uintptr_t)&i) == -1) 4764 err(EX_UNAVAILABLE, "getsockopt(%s)", "IP_FW_ADD"); 4765 if (!do_quiet) 4766 show_ipfw(rule, 0, 0); 4767} 4768 4769static void 4770zero(int ac, char *av[], int optname /* IP_FW_ZERO or IP_FW_RESETLOG */) 4771{ 4772 int rulenum; 4773 int failed = EX_OK; 4774 char const *name = optname == IP_FW_ZERO ? "ZERO" : "RESETLOG"; 4775 4776 av++; ac--; 4777 4778 if (!ac) { 4779 /* clear all entries */ 4780 if (do_cmd(optname, NULL, 0) < 0) 4781 err(EX_UNAVAILABLE, "setsockopt(IP_FW_%s)", name); 4782 if (!do_quiet) 4783 printf("%s.\n", optname == IP_FW_ZERO ? 4784 "Accounting cleared":"Logging counts reset"); 4785 4786 return; 4787 } 4788 4789 while (ac) { 4790 /* Rule number */ 4791 if (isdigit(**av)) { 4792 rulenum = atoi(*av); 4793 av++; 4794 ac--; 4795 if (do_cmd(optname, &rulenum, sizeof rulenum)) { 4796 warn("rule %u: setsockopt(IP_FW_%s)", 4797 rulenum, name); 4798 failed = EX_UNAVAILABLE; 4799 } else if (!do_quiet) 4800 printf("Entry %d %s.\n", rulenum, 4801 optname == IP_FW_ZERO ? 4802 "cleared" : "logging count reset"); 4803 } else { 4804 errx(EX_USAGE, "invalid rule number ``%s''", *av); 4805 } 4806 } 4807 if (failed != EX_OK) 4808 exit(failed); 4809} 4810 4811static void 4812flush(int force) 4813{ 4814 int cmd = do_pipe ? IP_DUMMYNET_FLUSH : IP_FW_FLUSH; 4815 4816 if (!force && !do_quiet) { /* need to ask user */ 4817 int c; 4818 4819 printf("Are you sure? [yn] "); 4820 fflush(stdout); 4821 do { 4822 c = toupper(getc(stdin)); 4823 while (c != '\n' && getc(stdin) != '\n') 4824 if (feof(stdin)) 4825 return; /* and do not flush */ 4826 } while (c != 'Y' && c != 'N'); 4827 printf("\n"); 4828 if (c == 'N') /* user said no */ 4829 return; 4830 } 4831 if (do_cmd(cmd, NULL, 0) < 0) 4832 err(EX_UNAVAILABLE, "setsockopt(IP_%s_FLUSH)", 4833 do_pipe ? "DUMMYNET" : "FW"); 4834 if (!do_quiet) 4835 printf("Flushed all %s.\n", do_pipe ? "pipes" : "rules"); 4836} 4837 4838/* 4839 * Free a the (locally allocated) copy of command line arguments. 4840 */ 4841static void 4842free_args(int ac, char **av) 4843{ 4844 int i; 4845 4846 for (i=0; i < ac; i++) 4847 free(av[i]); 4848 free(av); 4849} 4850 4851/* 4852 * This one handles all table-related commands 4853 * ipfw table N add addr[/masklen] [value] 4854 * ipfw table N delete addr[/masklen] 4855 * ipfw table N flush 4856 * ipfw table N list 4857 */ 4858static void 4859table_handler(int ac, char *av[]) 4860{ 4861 ipfw_table_entry ent; 4862 ipfw_table *tbl; 4863 int do_add; 4864 char *p; 4865 socklen_t l; 4866 uint32_t a; 4867 4868 ac--; av++; 4869 if (ac && isdigit(**av)) { 4870 ent.tbl = atoi(*av); 4871 ac--; av++; 4872 } else 4873 errx(EX_USAGE, "table number required"); 4874 NEED1("table needs command"); 4875 if (_substrcmp(*av, "add") == 0 || 4876 _substrcmp(*av, "delete") == 0) { 4877 do_add = **av == 'a'; 4878 ac--; av++; 4879 if (!ac) 4880 errx(EX_USAGE, "IP address required"); 4881 p = strchr(*av, '/'); 4882 if (p) { 4883 *p++ = '\0'; 4884 ent.masklen = atoi(p); 4885 if (ent.masklen > 32) 4886 errx(EX_DATAERR, "bad width ``%s''", p); 4887 } else 4888 ent.masklen = 32; 4889 if (lookup_host(*av, (struct in_addr *)&ent.addr) != 0) 4890 errx(EX_NOHOST, "hostname ``%s'' unknown", *av); 4891 ac--; av++; 4892 if (do_add && ac) 4893 ent.value = strtoul(*av, NULL, 0); 4894 else 4895 ent.value = 0; 4896 if (do_cmd(do_add ? IP_FW_TABLE_ADD : IP_FW_TABLE_DEL, 4897 &ent, sizeof(ent)) < 0) { 4898 /* If running silent, don't bomb out on these errors. */ 4899 if (!(do_quiet && (errno == (do_add ? EEXIST : ESRCH)))) 4900 err(EX_OSERR, "setsockopt(IP_FW_TABLE_%s)", 4901 do_add ? "ADD" : "DEL"); 4902 /* In silent mode, react to a failed add by deleting */ 4903 if (do_add) { 4904 do_cmd(IP_FW_TABLE_DEL, &ent, sizeof(ent)); 4905 if (do_cmd(IP_FW_TABLE_ADD, 4906 &ent, sizeof(ent)) < 0) 4907 err(EX_OSERR, 4908 "setsockopt(IP_FW_TABLE_ADD)"); 4909 } 4910 } 4911 } else if (_substrcmp(*av, "flush") == 0) { 4912 if (do_cmd(IP_FW_TABLE_FLUSH, &ent.tbl, sizeof(ent.tbl)) < 0) 4913 err(EX_OSERR, "setsockopt(IP_FW_TABLE_FLUSH)"); 4914 } else if (_substrcmp(*av, "list") == 0) { 4915 a = ent.tbl; 4916 l = sizeof(a); 4917 if (do_cmd(IP_FW_TABLE_GETSIZE, &a, (uintptr_t)&l) < 0) 4918 err(EX_OSERR, "getsockopt(IP_FW_TABLE_GETSIZE)"); 4919 l = sizeof(*tbl) + a * sizeof(ipfw_table_entry); 4920 tbl = malloc(l); 4921 if (tbl == NULL) 4922 err(EX_OSERR, "malloc"); 4923 tbl->tbl = ent.tbl; 4924 if (do_cmd(IP_FW_TABLE_LIST, tbl, (uintptr_t)&l) < 0) 4925 err(EX_OSERR, "getsockopt(IP_FW_TABLE_LIST)"); 4926 for (a = 0; a < tbl->cnt; a++) { 4927 printf("%s/%u %u\n", 4928 inet_ntoa(*(struct in_addr *)&tbl->ent[a].addr), 4929 tbl->ent[a].masklen, tbl->ent[a].value); 4930 } 4931 } else 4932 errx(EX_USAGE, "invalid table command %s", *av); 4933} 4934 4935/* 4936 * Called with the arguments (excluding program name). 4937 * Returns 0 if successful, 1 if empty command, errx() in case of errors. 4938 */ 4939static int 4940ipfw_main(int oldac, char **oldav) 4941{ 4942 int ch, ac, save_ac; 4943 char **av, **save_av; 4944 int do_acct = 0; /* Show packet/byte count */ 4945 4946#define WHITESP " \t\f\v\n\r" 4947 if (oldac == 0) 4948 return 1; 4949 else if (oldac == 1) { 4950 /* 4951 * If we are called with a single string, try to split it into 4952 * arguments for subsequent parsing. 4953 * But first, remove spaces after a ',', by copying the string 4954 * in-place. 4955 */ 4956 char *arg = oldav[0]; /* The string... */ 4957 int l = strlen(arg); 4958 int copy = 0; /* 1 if we need to copy, 0 otherwise */ 4959 int i, j; 4960 for (i = j = 0; i < l; i++) { 4961 if (arg[i] == '#') /* comment marker */ 4962 break; 4963 if (copy) { 4964 arg[j++] = arg[i]; 4965 copy = !index("," WHITESP, arg[i]); 4966 } else { 4967 copy = !index(WHITESP, arg[i]); 4968 if (copy) 4969 arg[j++] = arg[i]; 4970 } 4971 } 4972 if (!copy && j > 0) /* last char was a 'blank', remove it */ 4973 j--; 4974 l = j; /* the new argument length */ 4975 arg[j++] = '\0'; 4976 if (l == 0) /* empty string! */ 4977 return 1; 4978 4979 /* 4980 * First, count number of arguments. Because of the previous 4981 * processing, this is just the number of blanks plus 1. 4982 */ 4983 for (i = 0, ac = 1; i < l; i++) 4984 if (index(WHITESP, arg[i]) != NULL) 4985 ac++; 4986 4987 av = calloc(ac, sizeof(char *)); 4988 4989 /* 4990 * Second, copy arguments from cmd[] to av[]. For each one, 4991 * j is the initial character, i is the one past the end. 4992 */ 4993 for (ac = 0, i = j = 0; i < l; i++) 4994 if (index(WHITESP, arg[i]) != NULL || i == l-1) { 4995 if (i == l-1) 4996 i++; 4997 av[ac] = calloc(i-j+1, 1); 4998 bcopy(arg+j, av[ac], i-j); 4999 ac++; 5000 j = i + 1; 5001 } 5002 } else { 5003 /* 5004 * If an argument ends with ',' join with the next one. 5005 */ 5006 int first, i, l; 5007 5008 av = calloc(oldac, sizeof(char *)); 5009 for (first = i = ac = 0, l = 0; i < oldac; i++) { 5010 char *arg = oldav[i]; 5011 int k = strlen(arg); 5012 5013 l += k; 5014 if (arg[k-1] != ',' || i == oldac-1) { 5015 /* Time to copy. */ 5016 av[ac] = calloc(l+1, 1); 5017 for (l=0; first <= i; first++) { 5018 strcat(av[ac]+l, oldav[first]); 5019 l += strlen(oldav[first]); 5020 } 5021 ac++; 5022 l = 0; 5023 first = i+1; 5024 } 5025 } 5026 } 5027 5028 /* Set the force flag for non-interactive processes */ 5029 if (!do_force) 5030 do_force = !isatty(STDIN_FILENO); 5031 5032 /* Save arguments for final freeing of memory. */ 5033 save_ac = ac; 5034 save_av = av; 5035 5036 optind = optreset = 0; 5037 while ((ch = getopt(ac, av, "abcdefhnNqs:STtv")) != -1) 5038 switch (ch) { 5039 case 'a': 5040 do_acct = 1; 5041 break; 5042 5043 case 'b': 5044 comment_only = 1; 5045 do_compact = 1; 5046 break; 5047 5048 case 'c': 5049 do_compact = 1; 5050 break; 5051 5052 case 'd': 5053 do_dynamic = 1; 5054 break; 5055 5056 case 'e': 5057 do_expired = 1; 5058 break; 5059 5060 case 'f': 5061 do_force = 1; 5062 break; 5063 5064 case 'h': /* help */ 5065 free_args(save_ac, save_av); 5066 help(); 5067 break; /* NOTREACHED */ 5068 5069 case 'n': 5070 test_only = 1; 5071 break; 5072 5073 case 'N': 5074 do_resolv = 1; 5075 break; 5076 5077 case 'q': 5078 do_quiet = 1; 5079 break; 5080 5081 case 's': /* sort */ 5082 do_sort = atoi(optarg); 5083 break; 5084 5085 case 'S': 5086 show_sets = 1; 5087 break; 5088 5089 case 't': 5090 do_time = 1; 5091 break; 5092 5093 case 'T': 5094 do_time = 2; /* numeric timestamp */ 5095 break; 5096 5097 case 'v': /* verbose */ 5098 verbose = 1; 5099 break; 5100 5101 default: 5102 free_args(save_ac, save_av); 5103 return 1; 5104 } 5105 5106 ac -= optind; 5107 av += optind; 5108 NEED1("bad arguments, for usage summary ``ipfw''"); 5109 5110 /* 5111 * An undocumented behaviour of ipfw1 was to allow rule numbers first, 5112 * e.g. "100 add allow ..." instead of "add 100 allow ...". 5113 * In case, swap first and second argument to get the normal form. 5114 */ 5115 if (ac > 1 && isdigit(*av[0])) { 5116 char *p = av[0]; 5117 5118 av[0] = av[1]; 5119 av[1] = p; 5120 } 5121 5122 /* 5123 * optional: pipe or queue 5124 */ 5125 do_pipe = 0; 5126 if (_substrcmp(*av, "pipe") == 0) 5127 do_pipe = 1; 5128 else if (_substrcmp(*av, "queue") == 0) 5129 do_pipe = 2; 5130 if (do_pipe) { 5131 ac--; 5132 av++; 5133 } 5134 NEED1("missing command"); 5135 5136 /* 5137 * For pipes and queues we normally say 'pipe NN config' 5138 * but the code is easier to parse as 'pipe config NN' 5139 * so we swap the two arguments. 5140 */ 5141 if (do_pipe > 0 && ac > 1 && isdigit(*av[0])) { 5142 char *p = av[0]; 5143 5144 av[0] = av[1]; 5145 av[1] = p; 5146 } 5147 5148 if (_substrcmp(*av, "add") == 0) 5149 add(ac, av); 5150 else if (do_pipe && _substrcmp(*av, "config") == 0) 5151 config_pipe(ac, av); 5152 else if (_substrcmp(*av, "delete") == 0) 5153 delete(ac, av); 5154 else if (_substrcmp(*av, "flush") == 0) 5155 flush(do_force); 5156 else if (_substrcmp(*av, "zero") == 0) 5157 zero(ac, av, IP_FW_ZERO); 5158 else if (_substrcmp(*av, "resetlog") == 0) 5159 zero(ac, av, IP_FW_RESETLOG); 5160 else if (_substrcmp(*av, "print") == 0 || 5161 _substrcmp(*av, "list") == 0) 5162 list(ac, av, do_acct); 5163 else if (_substrcmp(*av, "set") == 0) 5164 sets_handler(ac, av); 5165 else if (_substrcmp(*av, "table") == 0) 5166 table_handler(ac, av); 5167 else if (_substrcmp(*av, "enable") == 0) 5168 sysctl_handler(ac, av, 1); 5169 else if (_substrcmp(*av, "disable") == 0) 5170 sysctl_handler(ac, av, 0); 5171 else if (_substrcmp(*av, "show") == 0) 5172 list(ac, av, 1 /* show counters */); 5173 else 5174 errx(EX_USAGE, "bad command `%s'", *av); 5175 5176 /* Free memory allocated in the argument parsing. */ 5177 free_args(save_ac, save_av); 5178 return 0; 5179} 5180 5181 5182static void 5183ipfw_readfile(int ac, char *av[]) 5184{ 5185#define MAX_ARGS 32 5186 char buf[BUFSIZ]; 5187 char *cmd = NULL, *filename = av[ac-1]; 5188 int c, lineno=0; 5189 FILE *f = NULL; 5190 pid_t preproc = 0; 5191 5192 filename = av[ac-1]; 5193 5194 while ((c = getopt(ac, av, "cfNnp:qS")) != -1) { 5195 switch(c) { 5196 case 'c': 5197 do_compact = 1; 5198 break; 5199 5200 case 'f': 5201 do_force = 1; 5202 break; 5203 5204 case 'N': 5205 do_resolv = 1; 5206 break; 5207 5208 case 'n': 5209 test_only = 1; 5210 break; 5211 5212 case 'p': 5213 cmd = optarg; 5214 /* 5215 * Skip previous args and delete last one, so we 5216 * pass all but the last argument to the preprocessor 5217 * via av[optind-1] 5218 */ 5219 av += optind - 1; 5220 ac -= optind - 1; 5221 av[ac-1] = NULL; 5222 fprintf(stderr, "command is %s\n", av[0]); 5223 break; 5224 5225 case 'q': 5226 do_quiet = 1; 5227 break; 5228 5229 case 'S': 5230 show_sets = 1; 5231 break; 5232 5233 default: 5234 errx(EX_USAGE, "bad arguments, for usage" 5235 " summary ``ipfw''"); 5236 } 5237 5238 if (cmd != NULL) 5239 break; 5240 } 5241 5242 if (cmd == NULL && ac != optind + 1) { 5243 fprintf(stderr, "ac %d, optind %d\n", ac, optind); 5244 errx(EX_USAGE, "extraneous filename arguments"); 5245 } 5246 5247 if ((f = fopen(filename, "r")) == NULL) 5248 err(EX_UNAVAILABLE, "fopen: %s", filename); 5249 5250 if (cmd != NULL) { /* pipe through preprocessor */ 5251 int pipedes[2]; 5252 5253 if (pipe(pipedes) == -1) 5254 err(EX_OSERR, "cannot create pipe"); 5255 5256 preproc = fork(); 5257 if (preproc == -1) 5258 err(EX_OSERR, "cannot fork"); 5259 5260 if (preproc == 0) { 5261 /* 5262 * Child, will run the preprocessor with the 5263 * file on stdin and the pipe on stdout. 5264 */ 5265 if (dup2(fileno(f), 0) == -1 5266 || dup2(pipedes[1], 1) == -1) 5267 err(EX_OSERR, "dup2()"); 5268 fclose(f); 5269 close(pipedes[1]); 5270 close(pipedes[0]); 5271 execvp(cmd, av); 5272 err(EX_OSERR, "execvp(%s) failed", cmd); 5273 } else { /* parent, will reopen f as the pipe */ 5274 fclose(f); 5275 close(pipedes[1]); 5276 if ((f = fdopen(pipedes[0], "r")) == NULL) { 5277 int savederrno = errno; 5278 5279 (void)kill(preproc, SIGTERM); 5280 errno = savederrno; 5281 err(EX_OSERR, "fdopen()"); 5282 } 5283 } 5284 } 5285 5286 while (fgets(buf, BUFSIZ, f)) { /* read commands */ 5287 char linename[10]; 5288 char *args[1]; 5289 5290 lineno++; 5291 sprintf(linename, "Line %d", lineno); 5292 setprogname(linename); /* XXX */ 5293 args[0] = buf; 5294 ipfw_main(1, args); 5295 } 5296 fclose(f); 5297 if (cmd != NULL) { 5298 int status; 5299 5300 if (waitpid(preproc, &status, 0) == -1) 5301 errx(EX_OSERR, "waitpid()"); 5302 if (WIFEXITED(status) && WEXITSTATUS(status) != EX_OK) 5303 errx(EX_UNAVAILABLE, 5304 "preprocessor exited with status %d", 5305 WEXITSTATUS(status)); 5306 else if (WIFSIGNALED(status)) 5307 errx(EX_UNAVAILABLE, 5308 "preprocessor exited with signal %d", 5309 WTERMSIG(status)); 5310 } 5311} 5312 5313int 5314main(int ac, char *av[]) 5315{ 5316 /* 5317 * If the last argument is an absolute pathname, interpret it 5318 * as a file to be preprocessed. 5319 */ 5320 5321 if (ac > 1 && av[ac - 1][0] == '/' && access(av[ac - 1], R_OK) == 0) 5322 ipfw_readfile(ac, av); 5323 else { 5324 if (ipfw_main(ac-1, av+1)) 5325 show_usage(); 5326 } 5327 return EX_OK; 5328} 5329