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