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