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