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