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