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