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